提交 8d3eb1ce 编写于 作者: 张磊

添加drawcontext和event相关文档

上级 720351a3
## DrawableContext ## DrawableContext
uni-app x 在 app 端提供 DrawableContext 绘制内容到 uvue 页面的`view`标签上。可用于绘制文本、形状等内容。
### 使用
#### 获取 DrawableContext 对象
DrawableContext 对象通过对象节点(INode)的`getDrawableContext()`方法获取
```vue
<template>
<view ref="drawable" style="width: 750rpx;height: 750rpx;">
</view>
</template>
<script>
export default {
onReady() {
var ctx = (this.$refs['drawable'] as INode).getDrawableContext()
}
}
</script>
```
#### 绘制内容
通过 DrawableContext 提供的 API 绘制文本、形状等内容
```ts
<script>
export default {
onReady() {
var ctx = (this.$refs['drawable'] as INode).getDrawableContext()
ctx.moveTo(50, 40);
ctx.lineTo(200, 40);
ctx.stroke();
}
}
</script>
```
#### 更新到画布
DrawableContext 在调用 API 之后不会主动更新到画布上,需要主动调用`update()`方法更新。
```ts
<script>
export default {
onReady() {
var ctx = (this.$refs['drawable'] as INode).getDrawableContext()
ctx.moveTo(50, 40);
ctx.lineTo(200, 40);
ctx.stroke();
ctx.update()
}
}
</script>
```
#### 清除画布内容
如果清除已经绘制的内容重新绘制,需要调用`reset()`方法清除内容再进行绘制。
```vue
<template>
<view ref="drawable" style="width: 750rpx;height: 750rpx;" @click="drawable">
</view>
</template>
<script>
export default {
data(){
return {
change:false
}
},
onReady() {
this.drawable()
},
methods:{
drawable(){
var ctx = (this.$refs['drawable'] as INode).getDrawableContext()
ctx.reset();
if(this.change) {
ctx.strokeStyle = "#33ff0000"
ctx.lineWidth = 10
}
this.change = !this.change
ctx.moveTo(50, 40);
ctx.lineTo(200, 40);
ctx.stroke();
ctx.update()
}
}
}
```
\ No newline at end of file
## UVUE Event ## UVUE Event
uni-app x 默认支持事件冒泡
### 简述
DOM事件主要有三个阶段:捕获阶段、目标阶段和冒泡阶段。
以点击事件为例,当触发点击时,首先从根节点逐级向下分发,直到监听点击事件的节点为止(捕获阶段);然后事件到达当前节点并触发点击事件(目标阶段);接着继续向上逐级触发父节点的点击事件,直到根节点为止(冒泡阶段)。
注意,虽然有3个阶段,但第2个阶段(“目标阶段”:事件到达了元素)并没有单独处理:捕获和冒泡阶段的处理程序都会在该阶段触发。
我们一般使用默认的事件注册机制,将事件注册到冒泡阶段,相对来说,大多数处理情况都在冒泡阶段。
uni-app x 目前暂时不支持事件的捕获阶段。
### 阻止冒泡
在事件回调中,可以通过调用`event.stopPropagation`方法阻止事件冒泡。
```ts
handleClick (event : MouseEvent) {
// 阻止继续冒泡.
event.stopPropagation();
}
```
### 阻止默认行为
在事件回调中,可以通过调用`event.preventDefault`方法阻止默认行为。`event.preventDefault`仅处理默认行为,事件冒泡不会被阻止。
```vue
<template>
<scroll-view style="flex: 1;">
<view style="width: 750rpx;height: 1750rpx;background-color: bisque;">
滑动框中区域修改进度并阻止滚动,滑动其余空白区域触发滚动
<view style="width: 750rpx;height: 40rpx; margin-top: 100rpx;border:5rpx;" @touchmove="slider">
<view ref="sliderNode" style="background-color: chocolate;width: 0rpx;height: 30rpx;"></view>
</view>
</view>
</scroll-view>
</template>
<script>
export default {
data() {
return {
sliderNode: null as INode | null
}
},
methods: {
slider(e : TouchEvent) {
if (this.sliderNode == null) {
this.sliderNode = this.$refs["sliderNode"] as INode
}
e.preventDefault() // 阻止外层scroll-view滚动行为
this.sliderNode!!.style?.setProperty('width', e.touches[0].screenX);
}
}
}
</script>
```
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册