当前位置: 首页 > 文档资料 > Rax 中文文档 >

Canvas

优质
小牛编辑
120浏览
2023-12-01

描述

  • 在支付宝小程序中依赖 canvas
  • 在 Web 和 Weex 中可以通过 ref 获取画布实例,在小程序中需要通过 createCanvasContext 和画布的 id 获取 canvas 上下文

安装

$ npm install rax-canvas --save

属性

属性类型默认值必填描述支持
styleObject-通过内联的方式设置画布样式
classNameObject-设置画布的样式
widthnumber0设置画布的宽度
heightnumber0设置画布的高度
onClickfunction-点击画布时触发的事件
onLongPressfunction-长按画布时触发的事件
onTouchStartfunction-触摸画布时触发的事件
onTouchMovefunction-手指在画布上移动时触发的事件
onTouchEndfunction-结束触摸时触发的事件
onTouchCancelfunction-触摸事件取消的时候触发的事件

注:基础属性、事件及图片含义见组件概述

方法

获取到画布实例后,可以使用实例上的方法。

注意

直接指定 width 或者 height 的优先级高于在 styleclassName 中指定的宽高。

示例

import { createElement, Component, render, createRef } from 'rax';
import Canvas from 'rax-canvas';
import DriverUniversal from "driver-universal"

class CanvasSample extends Component {
  constructor(props) {
    super(props);
    this.raxCanvasDemo = createRef()
  }
  componentDidMount() {
    const context = this.raxCanvasDemo.current.getContext();
    context.fillStyle = 'red';
    context.fillRect(0, 0, 100, 100);
  }

  render() {
    return <Canvas style={{
      width: 750,
      height: 750
    }} ref={this.raxCanvasDemo} />;
  }
}

render(<CanvasSample />, document.body, { driver: DriverUniversal });