当前位置:首页>区块链文章>区块链推广>Day 181/200 React Fabric.js 实现可拖拽变化的多边形

Day 181/200 React Fabric.js 实现可拖拽变化的多边形

1、需求实现Canvas绘制多边形。2、效果3、代码官方Demo是HTML版本的,我改写了React版本。// Copyright 2021 zhaoardenimport React, { useState,useCallback } from \'react\';import { fabric } from "fabric";import { Button } from \'antd\';class Polygon extends React.Component { construc

1、需求

实现Canvas绘制多边形。

2、效果

Day 181/200 React Fabric.js 实现可拖拽变化的多边形

3、代码

官方Demo是HTML版本的,我改写了React版本。

// Copyright 2021 zhaoardenimport React, { useState,useCallback } from \'react\';import { fabric } from \"fabric\";import { Button } from \'antd\';class Polygon extends React.Component {    constructor(props) {        super(props);        this.state={            canvas:{}        }    }    componentDidMount(){        console.log(\'componentDidMount\');        const canvas =  new fabric.Canvas(\'c\');        console.log(\'canvas\',canvas);        this.setState({canvas:canvas})        // console.log(\'state-canvas\',this.state.canvas);        var points = [{            x: 3, y: 4        }, {            x: 16, y: 3        }, {            x: 30, y: 5        }, {            x: 25, y: 55        }, {            x: 19, y: 44        }, {            x: 15, y: 30        }, {            x: 15, y: 55        }, {            x: 9, y: 55        }, {            x: 6, y: 53        }, {            x: -2, y: 55        }, {            x: -4, y: 40        }, {            x: 0, y: 20        }]        var polygon = new fabric.Polygon(points, {            left: 100,            top: 50,            fill: \'#D81B60\',            strokeWidth: 4,            stroke: \'green\',            scaleX: 4,            scaleY: 4,            objectCaching: false,            transparentCorners: false,            cornerColor: \'blue\',        });        canvas.viewportTransform = [0.7, 0, 0, 0.7, -50, 50];        canvas.add(polygon);    }    render() {        const controls={            display: \'inline-block\'        };        const {canvas}=this.state        // define a function that can locate the controls.        // this function will be used both for drawing and for interaction.        function polygonPositionHandler(dim, finalMatrix, fabricObject) {            var x = (fabricObject.points[this.pointIndex].x - fabricObject.pathOffset.x),                y = (fabricObject.points[this.pointIndex].y - fabricObject.pathOffset.y);            return fabric.util.transformPoint(                { x: x, y: y },                fabric.util.multiplyTransformMatrices(                    fabricObject.canvas.viewportTransform,                    fabricObject.calcTransformMatrix()                )            );        }        // define a function that will define what the control does        // this function will be called on every mouse move after a control has been        // clicked and is being dragged.        // The function receive as argument the mouse event, the current trasnform object        // and the current position in canvas coordinate        // transform.target is a reference to the current object being transformed,        function actionHandler(eventData, transform, x, y) {            var polygon = transform.target,                currentControl = polygon.controls[polygon.__corner],                mouseLocalPosition = polygon.toLocalPoint(new fabric.Point(x, y), \'center\', \'center\'),                polygonBaseSize = polygon._getNonTransformedDimensions(),                size = polygon._getTransformedDimensions(0, 0),                finalPointPosition = {                    x: mouseLocalPosition.x * polygonBaseSize.x / size.x + polygon.pathOffset.x,                    y: mouseLocalPosition.y * polygonBaseSize.y / size.y + polygon.pathOffset.y                };            polygon.points[currentControl.pointIndex] = finalPointPosition;            return true;        }        // define a function that can keep the polygon in the same position when we change its        // width/height/top/left.        function anchorWrapper(anchorIndex, fn) {            return function (eventData, transform, x, y) {                var fabricObject = transform.target,                    absolutePoint = fabric.util.transformPoint({                        x: (fabricObject.points[anchorIndex].x - fabricObject.pathOffset.x),                        y: (fabricObject.points[anchorIndex].y - fabricObject.pathOffset.y),                    }, fabricObject.calcTransformMatrix()),                    actionPerformed = fn(eventData, transform, x, y),                    newDim = fabricObject._setPositionDimensions({}),                    polygonBaseSize = fabricObject._getNonTransformedDimensions(),                    newX = (fabricObject.points[anchorIndex].x - fabricObject.pathOffset.x) / polygonBaseSize.x,                    newY = (fabricObject.points[anchorIndex].y - fabricObject.pathOffset.y) / polygonBaseSize.y;                fabricObject.setPositionByOrigin(absolutePoint, newX + 0.5, newY + 0.5);                return actionPerformed;            }        }        function Edit() {            console.log(\'canvas\',canvas);            console.log(\'Edit\');            // clone what are you copying since you            // may want copy and paste on different moment.            // and you do not want the changes happened            // later to reflect on the copy.            var poly = canvas.getObjects()[0];            canvas.setActiveObject(poly);            poly.edit = !poly.edit;            if (poly.edit) {                var lastControl = poly.points.length - 1;                poly.cornerStyle = \'circle\';                poly.cornerColor = \'rgba(0,0,255,0.5)\';                poly.controls = poly.points.reduce(function (acc, point, index) {                    acc[\'p\' + index] = new fabric.Control({                        positionHandler: polygonPositionHandler,                        actionHandler: anchorWrapper(index > 0 ? index - 1 : lastControl, actionHandler),                        actionName: \'modifyPolygon\',                        pointIndex: index                    });                    return acc;                }, {});            } else {                poly.cornerColor = \'blue\';                poly.cornerStyle = \'rect\';                poly.controls = fabric.Object.prototype.controls;            }            poly.hasBorders = !poly.edit;            canvas.requestRenderAll();        }        return <div>            <h1>Learn, {this.props.name}</h1>            <div className={controls}>                <p>                    <Button id=\"edit\" onClick={Edit}>Toggle editing polygon</Button>                </p>            </div>            <canvas id=\"c\" width=\"500\" height=\"400\" style={{border:\'1px solid #ccc\'}}></canvas>        </div>;    }}export default Polygon;

参考文档

http://fabricjs.com/docs/fabric.Polygon.html

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

给TA打赏
共{{data.count}}人
人已打赏
区块链推广

区块链智能合约(Soilidtiy)攻击--已知攻击类型

2021-12-25 8:45:33

区块链推广

《缠中说禅108课》3:你的喜好,你的死亡陷阱

2021-12-25 8:45:39

重要说明

本站资源大多来自网络,如有侵犯你的权益请联系管理员 区块链Bi站  或给邮箱发送邮件834379394@qq.com 我们会第一时间进行审核删除。 站内资源为网友个人学习或测试研究使用,未经原版权作者许可,禁止用于任何商业途径!请在下载24小时内删除!


如果你遇到支付完成,找不到下载链接,或者不能下载,或者解压失败,先不要忙,加客服主的QQ:834379394 (客服有可能有事情或者在睡觉不能及时的回复您,QQ留言后,请耐心等待即可!)

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索