Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello uni-app x
提交
1baf5f1c
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看板
提交
1baf5f1c
编写于
8月 08, 2024
作者:
辛宝Otto
🥊
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: 添加 canvas CreatePath2D 相关测试
上级
40d836cb
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
83 addition
and
42 deletion
+83
-42
pages/component/canvas/canvas.test.js
pages/component/canvas/canvas.test.js
+18
-6
pages/component/canvas/canvas.uvue
pages/component/canvas/canvas.uvue
+65
-36
未找到文件。
pages/component/canvas/canvas.test.js
浏览文件 @
1baf5f1c
...
...
@@ -40,10 +40,22 @@ describe('Canvas.uvue', () => {
expect
(
await
element
.
text
()).
toBe
(
'
true
'
)
})
// 配合安卓注释
// it('测试 createImage', async () => {
// await page.callMethod('onCreateImage');
// await page.waitFor(500) // 加载图片
// const element = await page.$('#testCreateImage')
// expect(await element.text()).toBe('true')
// })
it
(
'
测试 createImage
'
,
async
()
=>
{
if
(
process
.
env
.
uniTestPlatformInfo
.
toLowerCase
().
startsWith
(
'
web
'
))
{
// web skip
expect
(
true
).
toBe
(
true
)
}
else
{
await
page
.
callMethod
(
'
onCreateImage
'
);
await
page
.
waitFor
(
500
)
// 加载图片
const
element
=
await
page
.
$
(
'
#testCreateImage
'
)
expect
(
await
element
.
text
()).
toBe
(
'
true
'
)
}
})
it
(
'
测试 testCreatePath2D
'
,
async
()
=>
{
// onCreatePath2D
await
page
.
callMethod
(
'
onCreatePath2D
'
);
await
page
.
waitFor
(
50
)
const
element
=
await
page
.
$
(
'
#testCreatePath2D
'
)
expect
(
await
element
.
text
()).
toBe
(
'
true
'
)
})
})
pages/component/canvas/canvas.uvue
浏览文件 @
1baf5f1c
...
...
@@ -20,6 +20,11 @@
<text id="testCreateImage">{{testCreateImage}}</text>
</view>
<view class="text-group">
<text>测试 createPath2D 结果:</text>
<text id="testCreatePath2D">{{testCreatePath2D}}</text>
</view>
<!-- #ifdef WEB -->
<button class="canvas-drawing-button" @click="canvasToBlob">canvasToBlob</button>
<view>
...
...
@@ -32,6 +37,7 @@
<text>{{dataBase64.slice(0,22)}}...</text>
</view>
<button @click="onCreateImage">createImage</button>
<button @click="onCreatePath2D">createPath2D</button>
<view style="padding: 8px 10px;">CanvasContext API 演示</view>
<view class="grid-view">
<view class="grid-item" v-for="(name, index) in names" :key="index">
...
...
@@ -75,7 +81,8 @@
names: [...API_PATH, ...API_DRAW, ...API_STATE, ...API_PROPERTIES, "measureText", "path2D"].sort() as string[],
// 仅测试
testCreateCanvasContextAsyncSuccess: false,
testCreateImage: false
testCreateImage: false,
testCreatePath2D: false
}
},
onReady() {
...
...
@@ -122,13 +129,35 @@
canvasToDataURL() {
this.dataBase64 = this.canvasContext!.toDataURL()
},
onCreateImage() {
// let image = this.canvasContext!.createImage();
// image.src = "../../static/logo.png"
// image.onload = () => {
// this.testCreateImage = true
// this.renderingContext?.drawImage(image, 0, 0, 100, 100);
// }
onCreateImage() {
this.renderingContext!.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
let image = this.canvasContext!.createImage();
image.src = "../../static/logo.png"
image.onload = () => {
this.testCreateImage = true
this.renderingContext?.drawImage(image, 0, 0, 100, 100);
}
},
onCreatePath2D() {
this.renderingContext!.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
const context = this.renderingContext!
let path2D = this.canvasContext!.createPath2D()
this.testCreatePath2D = true
const amplitude = 64;
const wavelength = 64;
for (let i = 0; i < 5; i++) {
const x1 = 0 + (i * wavelength);
const y1 = 128;
const x2 = x1 + wavelength / 4;
const y2 = y1 - amplitude;
const x3 = x1 + 3 * wavelength / 4;
const y3 = y1 + amplitude;
const x4 = x1 + wavelength;
const y4 = y1;
context.moveTo(x1, y1);
path2D.bezierCurveTo(x2, y2, x3, y3, x4, y4);
}
context.stroke(path2D);
},
handleCanvasButton(name : string) {
this.renderingContext!.clearRect(0, 0, this.canvasWidth, this.canvasHeight)
...
...
@@ -258,10 +287,10 @@
this.setMiterLimit();
break;
case "textAlign":
this.textAlign();
break;
case "path2D":
this.path2DMethods();
this.textAlign();
break;
case "path2D":
this.path2DMethods();
break;
default:
break;
...
...
@@ -849,31 +878,31 @@
const textMetrics = context.measureText(text)
context.strokeText(text, 40, 100)
context.fillText("measure text width:" + textMetrics.width, 40, 80)
},
},
path2DMethods() {
const context = this.renderingContext!
context.beginPath()
const path2D = new Path2D();
const amplitude
= 64;
const wavelength = 64;
for (let i = 0; i < 5; i++) {
const x1 = 0 + (i * wavelength);
const y1 = 128;
const x2 = x1 + wavelength / 4;
const y2 = y1 - amplitude;
const x3 = x1 + 3 * wavelength / 4;
const y3 = y1 + amplitude;
const x4 = x1 + wavelength;
const y4 = y1;
context.moveTo(x1, y1);
path2D.bezierCurveTo(x2, y2, x3, y3, x4, y4);
}
context.stroke(path2D);
const path2DRect = new Path2D();
path2DRect.rect(10, 10, 100, 20);
context.fill(path2DRect);
const context = this.renderingContext!
context.beginPath()
const path2D = new Path2D();
const amplitude
= 64;
const wavelength = 64;
for (let i = 0; i < 5; i++) {
const x1 = 0 + (i * wavelength);
const y1 = 128;
const x2 = x1 + wavelength / 4;
const y2 = y1 - amplitude;
const x3 = x1 + 3 * wavelength / 4;
const y3 = y1 + amplitude;
const x4 = x1 + wavelength;
const y4 = y1;
context.moveTo(x1, y1);
path2D.bezierCurveTo(x2, y2, x3, y3, x4, y4);
}
context.stroke(path2D);
const path2DRect = new Path2D();
path2DRect.rect(10, 10, 100, 20);
context.fill(path2DRect);
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录