美文网首页
React中使用canvas画图

React中使用canvas画图

作者: 天海相接 | 来源:发表于2018-07-21 15:41 被阅读270次

前言:自从开始学习react,我再次确认了中西方思想差异。中国解决问题之道在于,利用最少、最好是现成的基本元素解决。西方就一个字:造。好了不说这些伪结论了。虽然在中国,百度在搜索引擎方面是老大,可是它的搜索能力却落后于潮流。完全搜不到react开发的项目。其它不那么出名的搜索引擎都实现了搜索基于react开发的项目。百度颓势初现。

正题
开发环境:package.json

{
  "name": "antd-react",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "antd": "^3.7.0",
    "install": "^0.12.1",
    "npm": "^6.2.0",
    "react": "^16.4.1",
    "react-amap": "^1.2.7",
    "react-dom": "^16.4.1",
    "react-player": "^1.6.4",
    "react-router-dom": "^4.3.1",
    "react-scripts": "1.1.4",
    "string.prototype.startswith": "^0.2.0"
  },
  "scripts": {
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",
    "test": "react-app-rewired test --env=jsdom",
    "eject": "react-scripts eject"
  },
  "devDependencies": {
    "babel-plugin-import": "^1.8.0",
    "react-app-rewired": "^1.5.2"
  }
}

获取真实DOM

都知道react虚拟DOM,既然用canvas绘图肯定要获取真实DOM。dome如下:

<canvas ref={this.canvas} width="780" height="1800">
    您的浏览器不支持canvas,请更换浏览器.
</canvas>
constructor(){
    super();
    this.canvas = React.createRef();
}
componentDidMount(){
    const canvas = this.canvas.current;
}

说明:经过这样这个对象的的canvas属性指向被被ref包装了一层的canvas元素。然后在元素被渲染之后通过canvas属性的current获取真实canvasDOM。
ref的用法(英文):https://reactjs.org/docs/refs-and-the-dom.html
ref的用法(中文):https://doc.react-china.org/docs/refs-and-the-dom.html

绘画技巧

获取当前CanvasRenderingContext2D实例的原型。

看看这个。

componentDidMount() {
    const canvas = this.canvas.current;
        if (canvas.getContext) {
            var ctx = canvas.getContext("2d");
            console.log(ctx);
            console.log(Object.getPrototypeOf(ctx));
        }
}

Object.getPrototypeOf()用于获取对象的原型,自己在浏览器上打印对比上面两的输出项的区别.

给CanvasRenderingContext2D原型添加方法

    componentDidMount() {
        const canvas = this.canvas.current;
        if (canvas.getContext) {
            var ctx = canvas.getContext("2d");
            console.log(ctx);
            console.log(Object.getPrototypeOf(ctx));
            (function () {
                Object.getPrototypeOf(ctx).Triangle = function (x, y, r) {
                    this.save();
                    this.translate(x, y);
                    this.rotate(r);
                    this.beginPath();
                    this.moveTo(0, 0);
                    this.lineTo(10, 0);
                    this.lineTo(0, 10);
                    this.lineTo(-10, 0);
                    this.closePath();
                    this.fill();
                    this.restore();
                }
                Object.getPrototypeOf(ctx).line = function (x, y, x1, y1) {
                    this.save();
                    this.beginPath();
                    this.moveTo(x, y);
                    this.lineTo(x1, y1);
                    this.stroke();
                    this.restore();
                }
            })();
            ctx.strokeStyle = "#7C8B8C";
            ctx.line(90, 130, 320, 210);
            ctx.Triangle(320, 210, -Math.PI * .4);

        }

    }

ok

相关文章

  • React中使用canvas画图

    前言:自从开始学习react,我再次确认了中西方思想差异。中国解决问题之道在于,利用最少、最好是现成的基本元素解决...

  • canvas

    在canvas上画图 所有在 中的画图必须使用js实现 第一步:创建一个Canvas绘图上下文 首先,我们需要创建...

  • AndroidUI常用类以及界面相关类

    1.android.graphic.Canvas Canvas类好比手机中的画纸,可以在Canvas上画图形或者图...

  • 微信小程序中canvas.drawImage画图

    描述:在页面加载是使用canvas.drawImage画图,点击页面下面按钮在已绘制好的图形上继续画图。使用ctx...

  • html5 Canvas画图5:曲线之arc

    本文属于《html5 Canvas画图系列教程》 在《html5 Canvas画图教程2:Canvas画线条 基础...

  • canvas画布

    canvas是定义状态实现画图的 canvas中无px单位;画直线:context.moveTo(100,200)...

  • Canvas

    1,什么是Canvas 画布的意思,简单理解就是用来作图的区域,实现画图必须需使用js作图 2.Canvas...

  • html5 Canvas画图7:曲线之quadraticCurv

    本文属于《html5 Canvas画图系列教程》 继续讲canvas中画曲线的方法,今天讲quadraticCur...

  • 2020-03-30 canvas画布自适应父div

    使用canvas画图时,我们希望当改变窗口大小,画布随窗口自适应改变画布大小。 我们要注意两个参数:canvas ...

  • 8个前端知识点:HTML5(canvas应用)

    (1) 应用 canvas笑脸 (2) 应用 canvas画图 (3) 应用 canvas变换 (4) 应用 ca...

网友评论

      本文标题:React中使用canvas画图

      本文链接:https://www.haomeiwen.com/subject/ibrayftx.html