Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
DCloud
hello uni-app x
提交
105362ef
H
hello uni-app x
项目概览
DCloud
/
hello uni-app x
通知
5704
Star
81
Fork
152
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
11
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
hello uni-app x
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
11
Issue
11
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
105362ef
编写于
8月 02, 2024
作者:
H
hdx
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
doodle: 简化示例,抽取公共逻辑
上级
148351ea
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
69 addition
and
70 deletion
+69
-70
pages/component/canvas/canvas/doodle.uvue
pages/component/canvas/canvas/doodle.uvue
+69
-70
未找到文件。
pages/component/canvas/canvas/doodle.uvue
浏览文件 @
105362ef
<template>
<canvas class="drawing" id="tablet" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd"></canvas>
<button @click="doClear()">清空</button>
<canvas class="drawing" id="tablet" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd"></canvas>
<button @click="doClear()">清空</button>
</template>
<script>
export default {
data() {
return {
lastPointX : 0,
lastPointY : 0
}
},
onShow() {
},
type Point = {
x : number
y : number
};
onReady() {
let element = uni.getElementById('tablet') as UniCanvasElement
let ctx = element.getContext("2d")
if ( ctx != null) {
const dpr = uni.getDeviceInfo().devicePixelRatio ?? 1;
element.width = element.offsetWidth * dpr
element.height = element.offsetHeight * dpr
ctx.scale(dpr, dpr)
export default {
data() {
return {
lastPointX: 0,
lastPointY: 0,
canvasElement: null as UniCanvasElement | null,
renderingContext: null as CanvasRenderingContext2D | null,
}
},
methods: {
touchStart(event: TouchEvent){
let element = uni.getElementById('tablet') as UniCanvasElement
const elRect = element.getBoundingClientRect();
const touch = event.touches[0];
const x = touch.clientX - elRect.left
const y = touch.clientY - elRect.top
this.lastPointX = x
this.lastPointY = y
},
touchMove(event: TouchEvent){
let element = uni.getElementById('tablet') as UniCanvasElement
const elRect = element.getBoundingClientRect();
const touch = event.touches[0];
const x = touch.clientX - elRect.left
const y = touch.clientY - elRect.top
let ctx = element.getContext("2d")
if ( null != ctx) {
ctx.lineWidth = 5;
ctx.lineCap = "round";
ctx.lineJoin = "square";
ctx.beginPath()
ctx.moveTo(this.lastPointX, this.lastPointY)
ctx.lineTo(x, y)
ctx.stroke()
}
this.lastPointX = x
this.lastPointY = y
},
touchEnd(event: TouchEvent){
},
doClear(){
let element = uni.getElementById('tablet') as UniCanvasElement
let ctx = element.getContext("2d")
if (null != ctx ) {
ctx.clearRect(0, 0, element.width, element.height)
}
}
}
}
},
onReady() {
this.canvasElement = uni.getElementById('tablet') as UniCanvasElement
this.renderingContext = this.canvasElement!.getContext("2d")!
const dpr = uni.getDeviceInfo().devicePixelRatio ?? 1;
this.canvasElement!.width = this.canvasElement!.offsetWidth * dpr
this.canvasElement!.height = this.canvasElement!.offsetHeight * dpr
this.renderingContext!.scale(dpr, dpr)
},
methods: {
touchStart(event : TouchEvent) {
const position = this.getPosition(event)
this.lastPointX = position.x
this.lastPointY = position.y
},
touchMove(event : TouchEvent) {
const position = this.getPosition(event)
const x = position.x
const y = position.y
const ctx = this.renderingContext!
ctx.lineWidth = 5
ctx.lineCap = "round"
ctx.lineJoin = "square"
ctx.beginPath()
ctx.moveTo(this.lastPointX, this.lastPointY)
ctx.lineTo(x, y)
ctx.stroke()
this.lastPointX = x
this.lastPointY = y
},
touchEnd(_ : TouchEvent) {
},
doClear() {
if (this.renderingContext != null) {
this.renderingContext!.clearRect(0, 0, this.canvasElement!.width, this.canvasElement!.height)
}
},
getPosition(event : TouchEvent) : Point {
const elRect = this.canvasElement!.getBoundingClientRect()
const touch = event.touches[0]
return {
x: touch.clientX - elRect.left,
y: touch.clientY - elRect.top
} as Point
}
}
}
</script>
<style>
.drawing {
width: 100%;
height: 500px;
background-color: lightgray;
margin-bottom: 15px;
}
.drawing {
width: 100%;
height: 500px;
background-color: lightgray;
margin-bottom: 15px;
}
</style>
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录