Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello uni-app x
提交
5be41118
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看板
提交
5be41118
编写于
8月 01, 2024
作者:
H
hdx
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
create-canvas-context-async: 提取一些公共逻辑,修正setFontSize
上级
ce0cf57b
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
48 addition
and
46 deletion
+48
-46
pages/API/create-canvas-context-async/create-canvas-context-async.uvue
...ate-canvas-context-async/create-canvas-context-async.uvue
+48
-46
未找到文件。
pages/API/create-canvas-context-async/create-canvas-context-async.uvue
浏览文件 @
5be41118
...
@@ -25,7 +25,7 @@
...
@@ -25,7 +25,7 @@
const API_PATH = ["arc", "arcTo", "bezierCurveTo", "quadraticCurve", "moveTo", "lineTo", "rect", "clip", "pattern"]
const API_PATH = ["arc", "arcTo", "bezierCurveTo", "quadraticCurve", "moveTo", "lineTo", "rect", "clip", "pattern"]
const API_DRAW = ["stroke", "strokeRect", "strokeText", "fill", "fillRect", "fillText", "drawImage", "drawImageLocal", "clearRect"]
const API_DRAW = ["stroke", "strokeRect", "strokeText", "fill", "fillRect", "fillText", "drawImage", "drawImageLocal", "clearRect"]
const API_STATE = ["beginPath", "closePath", "reset", "transform", "rotate", "resetTransform", "save", "restore", "scale", "translate"]
const API_STATE = ["beginPath", "closePath", "reset", "transform", "rotate", "resetTransform", "save", "restore", "scale", "translate"]
const API_PROPERTIES = ["setLineCap", "setLineJoin", "setLineWidth", "setMiterLimit", "setFillStyle", "setStrokeStyle", "setGlobalAlpha", "lineDash", "linearGradient", "radialGradient", "textAlign"]
const API_PROPERTIES = ["setLineCap", "setLineJoin", "setLineWidth", "setMiterLimit", "setFillStyle", "setStrokeStyle", "setGlobalAlpha", "
setFontSize", "
lineDash", "linearGradient", "radialGradient", "textAlign"]
return {
return {
title: 'Context2D',
title: 'Context2D',
names: [...API_PATH, ...API_DRAW, ...API_STATE, ...API_PROPERTIES, "measureText"] as string[],
names: [...API_PATH, ...API_DRAW, ...API_STATE, ...API_PROPERTIES, "measureText"] as string[],
...
@@ -203,7 +203,7 @@
...
@@ -203,7 +203,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
context.beginPath()
context.beginPath()
context.lineWidth = 2
context.lineWidth = 2
context.arc(75, 75, 50, 0, Math.PI * 2, true)
context.arc(75, 75, 50, 0, Math.PI * 2, true)
...
@@ -221,7 +221,7 @@
...
@@ -221,7 +221,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
context.beginPath()
context.beginPath()
context.moveTo(150, 20)
context.moveTo(150, 20)
context.arcTo(150, 100, 50, 20, 30)
context.arcTo(150, 100, 50, 20, 30)
...
@@ -252,7 +252,7 @@
...
@@ -252,7 +252,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
// First path
// First path
context.beginPath()
context.beginPath()
context.strokeStyle = "blue"
context.strokeStyle = "blue"
...
@@ -273,7 +273,7 @@
...
@@ -273,7 +273,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
context.beginPath()
context.beginPath()
context.moveTo(150, 0)
context.moveTo(150, 0)
context.lineTo(150, 150)
context.lineTo(150, 150)
...
@@ -296,7 +296,7 @@
...
@@ -296,7 +296,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
context.beginPath()
context.beginPath()
context.moveTo(50, 20)
context.moveTo(50, 20)
context.bezierCurveTo(230, 30, 150, 60, 50, 100)
context.bezierCurveTo(230, 30, 150, 60, 50, 100)
...
@@ -320,7 +320,7 @@
...
@@ -320,7 +320,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
// 绘制黄色背景
// 绘制黄色背景
context.beginPath()
context.beginPath()
context.fillStyle = "#ff6"
context.fillStyle = "#ff6"
...
@@ -344,7 +344,7 @@
...
@@ -344,7 +344,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
// Create circular clipping region
// Create circular clipping region
context.beginPath();
context.beginPath();
context.arc(100, 75, 50, 0, Math.PI * 2, true)
context.arc(100, 75, 50, 0, Math.PI * 2, true)
...
@@ -362,7 +362,7 @@
...
@@ -362,7 +362,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
context.beginPath()
context.beginPath()
context.lineWidth = 10
context.lineWidth = 10
context.moveTo(20, 20)
context.moveTo(20, 20)
...
@@ -378,11 +378,10 @@
...
@@ -378,11 +378,10 @@
const image = new Image(100, 100)
const image = new Image(100, 100)
image.src = '../../../static/api.png';
image.src = '../../../static/api.png';
// Only use the image after it's loaded
image.onload = () => {
image!.onload = () => {
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
const pattern = context.createPattern(image
!
, "repeat")
const pattern = context.createPattern(image, "repeat")
context.fillStyle = pattern
context.fillStyle = pattern
context.fillRect(0, 0, 100, 100)
context.fillRect(0, 0, 100, 100)
context.restore()
context.restore()
...
@@ -392,7 +391,7 @@
...
@@ -392,7 +391,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
// Create a linear gradient
// Create a linear gradient
// The start gradient point is at x=20, y=0
// The start gradient point is at x=20, y=0
// The end gradient point is at x=220, y=0
// The end gradient point is at x=220, y=0
...
@@ -413,7 +412,7 @@
...
@@ -413,7 +412,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
// Create a radial gradient
// Create a radial gradient
// The inner circle is at x=110, y=90, with radius=30
// The inner circle is at x=110, y=90, with radius=30
// The outer circle is at x=100, y=100, with radius=70
// The outer circle is at x=100, y=100, with radius=70
...
@@ -434,7 +433,7 @@
...
@@ -434,7 +433,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
context.beginPath()
context.beginPath()
context.rect(20, 20, 150, 100)
context.rect(20, 20, 150, 100)
context.strokeStyle = '#00ff00'
context.strokeStyle = '#00ff00'
...
@@ -446,7 +445,7 @@
...
@@ -446,7 +445,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
context.fillStyle = "green"
context.fillStyle = "green"
context.fillRect(20, 10, 150, 100)
context.fillRect(20, 10, 150, 100)
...
@@ -456,7 +455,7 @@
...
@@ -456,7 +455,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
console.log("fillText")
console.log("fillText")
context.strokeStyle = '#ff0000'
context.strokeStyle = '#ff0000'
...
@@ -485,7 +484,7 @@
...
@@ -485,7 +484,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
context.beginPath()
context.beginPath()
context.moveTo(0, 0)
context.moveTo(0, 0)
context.lineTo(300, 150)
context.lineTo(300, 150)
...
@@ -497,7 +496,7 @@
...
@@ -497,7 +496,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
context.beginPath()
context.beginPath()
context.moveTo(20, 20)
context.moveTo(20, 20)
context.lineTo(20, 100)
context.lineTo(20, 100)
...
@@ -510,7 +509,7 @@
...
@@ -510,7 +509,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
context.beginPath()
context.beginPath()
context.moveTo(20, 20)
context.moveTo(20, 20)
context.lineTo(20, 100)
context.lineTo(20, 100)
...
@@ -524,7 +523,7 @@
...
@@ -524,7 +523,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
context.strokeStyle = "green"
context.strokeStyle = "green"
context.strokeRect(20, 10, 160, 100)
context.strokeRect(20, 10, 160, 100)
...
@@ -534,7 +533,7 @@
...
@@ -534,7 +533,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
context.font = "10px serif"
context.font = "10px serif"
context.strokeText("Hello world", 50, 90)
context.strokeText("Hello world", 50, 90)
...
@@ -548,7 +547,7 @@
...
@@ -548,7 +547,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
// Point of transform origin
// Point of transform origin
context.arc(0, 0, 5, 0, 2 * Math.PI, true)
context.arc(0, 0, 5, 0, 2 * Math.PI, true)
context.fillStyle = "blue"
context.fillStyle = "blue"
...
@@ -571,7 +570,7 @@
...
@@ -571,7 +570,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
// Scaled rectangle
// Scaled rectangle
context.scale(9, 3)
context.scale(9, 3)
context.fillStyle = "red"
context.fillStyle = "red"
...
@@ -618,7 +617,7 @@
...
@@ -618,7 +617,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
// Moved square
// Moved square
context.translate(110, 30)
context.translate(110, 30)
context.fillStyle = "red"
context.fillStyle = "red"
...
@@ -635,7 +634,7 @@
...
@@ -635,7 +634,7 @@
},
},
save() {
save() {
const context = this.canvasContext!
const context = this.canvasContext!
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
context.save()
context.save()
context.beginPath()
context.beginPath()
context.strokeStyle = '#00ff00'
context.strokeStyle = '#00ff00'
...
@@ -653,7 +652,7 @@
...
@@ -653,7 +652,7 @@
},
},
restore() {
restore() {
const context = this.canvasContext!;
const context = this.canvasContext!;
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
);
this.clearCanvasRect(
);
[3, 2, 1].forEach((item) => {
[3, 2, 1].forEach((item) => {
context.save()
context.save()
context.beginPath()
context.beginPath()
...
@@ -665,8 +664,8 @@
...
@@ -665,8 +664,8 @@
},
},
drawImageLocal() {
drawImageLocal() {
const context = this.canvasContext!
const context = this.canvasContext!
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
le
t image = new Image(100, 100)
cons
t image = new Image(100, 100)
image.src = '../../../static/uni.png'
image.src = '../../../static/uni.png'
image.onload = () => {
image.onload = () => {
context.drawImage(image, 0, 0, 100, 100)
context.drawImage(image, 0, 0, 100, 100)
...
@@ -674,8 +673,8 @@
...
@@ -674,8 +673,8 @@
},
},
drawImage() {
drawImage() {
const context = this.canvasContext!
const context = this.canvasContext!
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
le
t image = new Image(100, 100);
cons
t image = new Image(100, 100);
image.src = 'https://web-ext-storage.dcloud.net.cn/uni-app-x/hello-uniappx-qrcode.png'
image.src = 'https://web-ext-storage.dcloud.net.cn/uni-app-x/hello-uniappx-qrcode.png'
image.onload = () => {
image.onload = () => {
context.drawImage(image, 0, 0, 100, 100)
context.drawImage(image, 0, 0, 100, 100)
...
@@ -685,7 +684,7 @@
...
@@ -685,7 +684,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
context.beginPath()
context.beginPath()
context.rect(20, 20, 150, 100)
context.rect(20, 20, 150, 100)
context.stroke()
context.stroke()
...
@@ -696,7 +695,7 @@
...
@@ -696,7 +695,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
// Quadratic Bézier curve
// Quadratic Bézier curve
context.beginPath()
context.beginPath()
context.moveTo(50, 20)
context.moveTo(50, 20)
...
@@ -722,7 +721,7 @@
...
@@ -722,7 +721,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
// Draw a rotated rectangle
// Draw a rotated rectangle
context.rotate((45 * Math.PI) / 180)
context.rotate((45 * Math.PI) / 180)
context.fillRect(60, 0, 100, 30)
context.fillRect(60, 0, 100, 30)
...
@@ -738,7 +737,7 @@
...
@@ -738,7 +737,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
context.transform(1, 0.2, 0.8, 1, 0, 0)
context.transform(1, 0.2, 0.8, 1, 0, 0)
context.fillRect(0, 0, 100, 100)
context.fillRect(0, 0, 100, 100)
...
@@ -748,7 +747,7 @@
...
@@ -748,7 +747,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
);
this.clearCanvasRect(
);
['#fef957', 'rgb(242,159,63)', 'rgb(242,117,63)', '#e87e51'].forEach((item : string, index : number) => {
['#fef957', 'rgb(242,159,63)', 'rgb(242,117,63)', '#e87e51'].forEach((item : string, index : number) => {
context.fillStyle = item
context.fillStyle = item
context.beginPath()
context.beginPath()
...
@@ -762,7 +761,7 @@
...
@@ -762,7 +761,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
);
this.clearCanvasRect(
);
['#fef957', 'rgb(242,159,63)', 'rgb(242,117,63)', '#e87e51'].forEach((item : string, index : number) => {
['#fef957', 'rgb(242,159,63)', 'rgb(242,117,63)', '#e87e51'].forEach((item : string, index : number) => {
context.strokeStyle = item
context.strokeStyle = item
context.beginPath()
context.beginPath()
...
@@ -776,7 +775,7 @@
...
@@ -776,7 +775,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
context.fillStyle = '#000000';
context.fillStyle = '#000000';
[1, 0.5, 0.1].forEach((item : number, index : number) => {
[1, 0.5, 0.1].forEach((item : number, index : number) => {
context.globalAlpha = item
context.globalAlpha = item
...
@@ -793,7 +792,7 @@
...
@@ -793,7 +792,7 @@
context.save();
context.save();
[10, 20, 30, 40].forEach((item : number, index : number) => {
[10, 20, 30, 40].forEach((item : number, index : number) => {
// context.fontSize(item)
context.font = item + 'px serif'
context.fillText('Hello, world', 20, 20 + 40 * index)
context.fillText('Hello, world', 20, 20 + 40 * index)
})
})
...
@@ -803,7 +802,7 @@
...
@@ -803,7 +802,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
context.lineWidth = 10;
context.lineWidth = 10;
['butt', 'round', 'square'].forEach((item : string, index : number) => {
['butt', 'round', 'square'].forEach((item : string, index : number) => {
context.beginPath()
context.beginPath()
...
@@ -819,7 +818,7 @@
...
@@ -819,7 +818,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
context.lineWidth = 10;
context.lineWidth = 10;
['bevel', 'round', 'miter'].forEach((item : string, index : number) => {
['bevel', 'round', 'miter'].forEach((item : string, index : number) => {
context.beginPath()
context.beginPath()
...
@@ -836,7 +835,7 @@
...
@@ -836,7 +835,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
);
this.clearCanvasRect(
);
[2, 4, 6, 8, 10].forEach((item : number, index : number) => {
[2, 4, 6, 8, 10].forEach((item : number, index : number) => {
context.beginPath()
context.beginPath()
context.lineWidth = item
context.lineWidth = item
...
@@ -851,7 +850,7 @@
...
@@ -851,7 +850,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save();
context.save();
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
context.setLineDash([4, 16])
context.setLineDash([4, 16])
// Dashed line with no offset
// Dashed line with no offset
...
@@ -874,7 +873,7 @@
...
@@ -874,7 +873,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
context.lineWidth = 4;
context.lineWidth = 4;
[2, 4, 6, 8, 10].forEach((item : number, index : number) => {
[2, 4, 6, 8, 10].forEach((item : number, index : number) => {
context.beginPath()
context.beginPath()
...
@@ -891,7 +890,7 @@
...
@@ -891,7 +890,7 @@
const context = this.canvasContext!
const context = this.canvasContext!
context.save()
context.save()
context.clearRect(0, 0, this.canvasWidth, this.canvasHeight
)
this.clearCanvasRect(
)
const text = "uni-app x,是下一代 uni-app,是一个跨平台应用开发引擎"
const text = "uni-app x,是下一代 uni-app,是一个跨平台应用开发引擎"
context.font = "20px 宋体"
context.font = "20px 宋体"
...
@@ -903,6 +902,9 @@
...
@@ -903,6 +902,9 @@
context.fillText("measure text width:" + textMetrics.width, 40, 80)
context.fillText("measure text width:" + textMetrics.width, 40, 80)
context.restore()
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录