Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello uni-app x
提交
bc247ad8
H
hello uni-app x
项目概览
DCloud
/
hello uni-app x
通知
5995
Star
90
Fork
162
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
18
列表
看板
标记
里程碑
合并请求
1
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hello uni-app x
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
18
Issue
18
列表
看板
标记
里程碑
合并请求
1
合并请求
1
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
bc247ad8
编写于
8月 05, 2024
作者:
辛宝Otto
🥊
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: 添加 canvas 同步异步测试方法
上级
c17b6819
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
143 addition
and
38 deletion
+143
-38
pages/component/canvas/canvas.test.js
pages/component/canvas/canvas.test.js
+25
-5
pages/component/canvas/canvas.uvue
pages/component/canvas/canvas.uvue
+118
-33
未找到文件。
pages/component/canvas/canvas.test.js
浏览文件 @
bc247ad8
let
page
let
page
beforeAll
(
async
()
=>
{
beforeAll
(
async
()
=>
{
if
(
!
process
.
env
.
uniTestPlatformInfo
.
toLowerCase
().
startsWith
(
'
web
'
))
{
// if (!process.env.uniTestPlatformInfo.toLowerCase().startsWith('web')) {
return
// return
}
//
}
page
=
await
program
.
reLaunch
(
'
/pages/component/canvas/canvas
'
)
page
=
await
program
.
reLaunch
(
'
/pages/component/canvas/canvas
'
)
await
page
.
waitFor
(
20
00
)
await
page
.
waitFor
(
5
00
)
})
})
describe
(
'
Canvas.uvue
'
,
()
=>
{
describe
(
'
Canvas.uvue
'
,
()
=>
{
...
@@ -18,6 +18,26 @@ describe('Canvas.uvue', () => {
...
@@ -18,6 +18,26 @@ describe('Canvas.uvue', () => {
expect
(
testToBlobResult
).
toBe
(
true
)
expect
(
testToBlobResult
).
toBe
(
true
)
expect
(
testToDataURLResult
).
toBe
(
true
)
expect
(
testToDataURLResult
).
toBe
(
true
)
}
else
{
// app skip
expect
(
true
).
toBe
(
true
)
}
}
})
})
it
(
"
测试异步方式
"
,
async
()
=>
{
await
page
.
callMethod
(
'
useAsync
'
);
const
{
testCanvasContext
,
testToDataURLResult
}
=
await
page
.
data
()
expect
(
testCanvasContext
).
toBe
(
true
)
await
page
.
callMethod
(
'
canvasToDataURL
'
);
expect
(
testToDataURLResult
).
toBe
(
true
)
})
it
(
"
测试同步方式
"
,
async
()
=>
{
await
page
.
callMethod
(
'
useSync
'
);
const
data
=
await
page
.
data
()
expect
(
data
.
testCanvasContext
).
toBe
(
true
)
await
page
.
callMethod
(
'
canvasToDataURL
'
);
expect
(
data
.
testToDataURLResult
).
toBe
(
true
)
})
})
})
pages/component/canvas/canvas.uvue
浏览文件 @
bc247ad8
<template>
<template>
<view class="page" id="page-canvas">
<view class="page" id="page-canvas">
<canvas id="canvas" class="canvas-element"></canvas>
<canvas id="canvas" class="canvas-element"></canvas>
<scroll-view class="scroll-view">
<scroll-view class="scroll-view">
<!-- #ifdef WEB -->
<button class="canvas-drawing-button" id="useAsync" @click="useAsync">使用异步方式设置(跨平台推荐)</button>
<button class="canvas-drawing-button" @click="canvasToBlob">canvasToBlob</button>
<button class="canvas-drawing-button" id="useSync" @click="useSync">使用同步方式设置</button>
<!-- #endif -->
<!-- #ifdef WEB -->
<button class="canvas-drawing-button" @click="canvasToDataURL">canvasToDataURL</button>
<button class="canvas-drawing-button" @click="canvasToBlob">canvasToBlob</button>
<text>{{dataBase64}}</text>
<view>
<text>testToBlobResult: {{testToBlobResult}}</text>
</view>
<!-- #endif -->
<button class="canvas-drawing-button" id="toDataURL" @click="canvasToDataURL">canvasToDataURL</button>
<view v-if='dataBase64.length>0' :style="{
width: canvasWidth,
height: canvasHeight,
padding:'10px',
}">
<text>canvasToDataURL 返回结果:</text>
<text>{{dataBase64.slice(0,35)}}...</text>
<view>
<text>canvasToDataURL 转图片预览:</text>
</view>
<image mode='aspectFit' :src="dataBase64"></image>
</view>
</scroll-view>
</scroll-view>
</view>
</view>
</template>
</template>
...
@@ -23,43 +39,112 @@
...
@@ -23,43 +39,112 @@
export default {
export default {
data() {
data() {
return {
return {
title: 'Context2D',
title: 'Context2D',
canvas: null as UniCanvasElement | null,
//
canvas: null as UniCanvasElement | null,
canvasContext: null as CanvasRenderingContext2D | null,
//
canvasContext: null as CanvasRenderingContext2D | null,
canvasWidth: 0,
canvasWidth: 0,
canvasHeight: 0,
canvasHeight: 0,
dataBase64: '',
dataBase64: '',
// 仅测试
// 仅测试
testCanvasContext: false,
testToBlobResult: false,
testToBlobResult: false,
testToDataURLResult: false
testToDataURLResult: false
}
}
},
},
onReady() {
onReady() {
let canvas = uni.getElementById("canvas") as UniCanvasElement
// this.useAsync()
this.canvasContext = canvas.getContext("2d");
},
hidpi(canvas);
methods: {
this.canvasWidth = this.canvasContext!.canvas.width;
this.canvasHeight = this.canvasContext!.canvas.height;
// #ifdef WEB
// #ifdef WEB
canvas.toBlob((blob : Blob) => {
canvasToBlob() {
this.testToBlobResult = (blob.size > 0 && blob.type == 'image/jpeg')
this.canvas!.toBlob((blob : Blob) => {
}, 'image/jpeg', 0.95)
this.testToBlobResult = (blob.size > 0 && blob.type == 'image/jpeg')
}, 'image/jpeg', 0.95)
},
// #endif
// #endif
this.testToDataURLResult = canvas.toDataURL().startsWith('data:image/png;base64')
canvasToDataURL() {
this.dataBase64 = this.canvas!.toDataURL()
this.canvas = canvas;
},
},
useAsync() {
methods: {
// #ifdef WEB
// HBuilderX 4.25+
canvasToBlob() {
// 异步调用方式, 跨平台写法
this.canvas!.toBlob((blob : Blob) => {
uni.createCanvasContextAsync({
this.testToBlobResult = (blob.size > 0 && blob.type == 'image/jpeg')
id: 'canvas',
}, 'image/jpeg', 0.95)
component: this,
},
success: (context : CanvasContext) => {
// #endif
const canvasContext = context.getContext('2d')!;
canvasToDataURL() {
this.canvasContext = canvasContext
this.dataBase64 = this.canvas!.toDataURL()
this.testCanvasContext = true
const canvas = canvasContext.canvas;
canvasContext.save()
hidpi(canvas);
this.canvasWidth = canvas.width;
this.canvasHeight = canvas.height;
this.drawImage()
this.testToDataURLResult = canvas.toDataURL().startsWith('data:image/png;base64')
this.canvas = canvas;
}
})
},
useSync() {
let canvas = uni.getElementById("canvas") as UniCanvasElement
this.canvasContext = canvas.getContext("2d")
this.testCanvasContext = true
this.canvasContext!.save()
hidpi(canvas);
this.canvasWidth = canvas.width;
this.canvasHeight = canvas.height;
this.arc()
// #ifdef WEB
canvas.toBlob((blob : Blob) => {
this.testToBlobResult = (blob.size > 0 && blob.type == 'image/jpeg')
}, 'image/jpeg', 0.95)
// #endif
this.testToDataURLResult = canvas.toDataURL().startsWith('data:image/png;base64')
this.canvas = canvas;
},
arc() {
const context = this.canvasContext!
this.clearCanvasRect()
context.beginPath()
context.lineWidth = 2
context.arc(75, 75, 50, 0, Math.PI * 2, true)
context.moveTo(110, 75)
context.arc(75, 75, 35, 0, Math.PI, false)
context.moveTo(65, 65)
context.arc(60, 65, 5, 0, Math.PI * 2, true)
context.moveTo(95, 65)
context.arc(90, 65, 5, 0, Math.PI * 2, true)
context.stroke()
context.restore()
},
drawImage() {
const context = this.canvasContext!
this.clearCanvasRect()
const text = "uni-app x,是下一代 uni-app,是一个跨平台应用开发引擎"
context.font = "20px 宋体"
context.fillStyle = "red"
context.fillText(text, 0, 60)
const textMetrics = context.measureText(text)
context.strokeText(text, 40, 100)
context.fillText("measure text width:" + textMetrics.width, 40, 80)
context.restore()
},
clearCanvasRect() {
this.canvasContext!.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
}
}
}
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录