UNPKG

@jdcfe/yep-react

Version:

一套移动端的React组件库

167 lines (141 loc) 6.52 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } import * as React from 'react'; import classNames from 'classnames'; var Scratch = /*#__PURE__*/function (_React$PureComponent) { _inherits(Scratch, _React$PureComponent); var _super = _createSuper(Scratch); function Scratch(props) { var _this; _classCallCheck(this, Scratch); _this = _super.call(this, props); _this.state = { isAreaShow: true, isArea2Show: false }; _this.touchStart = _this.touchStart.bind(_assertThisInitialized(_this)); _this.touchMove = _this.touchMove.bind(_assertThisInitialized(_this)); _this.touchEnd = _this.touchEnd.bind(_assertThisInitialized(_this)); return _this; } _createClass(Scratch, [{ key: "componentDidMount", value: function componentDidMount() { var wrap_canvas = document.getElementById('wrap_canvas'); var icanvas = document.getElementById('myCanvas'); // if (wrap_canvas && icanvas) { var iwidth = wrap_canvas.clientWidth; var iheight = wrap_canvas.clientHeight; icanvas.width = iwidth; icanvas.height = iheight; // var drawMode = false; var context = icanvas.getContext('2d'); context.lineCap = 'round'; context.lineJoin = 'round'; // context.fillStyle = 'rgb(193,193,193)'; // context.fillRect(0, 0, iwidth, iheight); var imgObj = new Image(); imgObj.crossOrigin = 'Anonymous'; imgObj.src = this.props.scratchImgUrl; //待图片加载完后,将其显示在canvas上 imgObj.onload = function () { context.drawImage(imgObj, 0, 0, iwidth, iheight); //this即是imgObj,保持图片的原始大小:470*480 //ctx.drawImage(this, 0, 0,1024,768); //改变图片的大小到1024*768 // 在源图像外显示目标图像。只有源图像外的目标图像部分会被显示,源图像是透明的。(源图像:刮痕区域,目标图像:带图片的初始canvas) context.globalCompositeOperation = 'destination-out'; }; // context.font = '24px 微软雅黑'; // context.fillStyle = '#454444'; // var txt = '刮这里'; // context.fillText(txt, iwidth / 2 - context.measureText(txt).width / 2, iheight / 2 + 9); // context.globalCompositeOperation = 'destination-out'; context.lineWidth = 30; // } } }, { key: "touchStart", value: function touchStart(e) { var icanvas = document.getElementById('myCanvas'); var context = icanvas.getContext('2d'); context.beginPath(); var rect = icanvas.getBoundingClientRect(); context.moveTo(e.changedTouches[0].clientX - rect.left, e.changedTouches[0].clientY - rect.top); // $("#area2").show(); } }, { key: "touchMove", value: function touchMove(e) { // $icanvas.off('touchmove'); e.persist(); this.setState({ isArea2Show: true }); // var e = event; var wrap_canvas = document.getElementById('wrap_canvas'); var icanvas = document.getElementById('myCanvas'); var context = icanvas.getContext('2d'); // if (this.state.down) { var rect = icanvas.getBoundingClientRect(); context.lineTo(e.changedTouches[0].clientX - rect.left, e.changedTouches[0].clientY - rect.top); context.stroke(); // } var iwidth = wrap_canvas.clientWidth; var iheight = wrap_canvas.clientHeight; var imgData = context.getImageData(0, 0, iwidth, iheight), pixles = imgData.data, transPixs = []; for (var i = 0, j = pixles.length; i < j; i += 4) { var a = pixles[i + 3]; if (a < 128) { //@ts-ignore transPixs.push(i); } } //@ts-ignore if (transPixs.length / (pixles.length / 4) * 100 > (this.props.resPercent >= 0 ? this.props.resPercent : 60)) { this.props.resCallback(); this.setState({ isAreaShow: false }); } e.preventDefault && e.preventDefault(); e.returnValue = false; e.stopPropagation && e.stopPropagation(); return false; } }, { key: "touchEnd", value: function touchEnd() {// $icanvas.off('touchend'); // this.setState({ // down: false, // }); } }, { key: "render", value: function render() { var _this$props = this.props, prefixCls = _this$props.prefixCls, className = _this$props.className, style = _this$props.style; var cls = classNames(prefixCls, className); return /*#__PURE__*/React.createElement("div", { className: cls, style: style }, this.state.isAreaShow && /*#__PURE__*/React.createElement("div", { className: "area", id: "wrap_canvas" }, /*#__PURE__*/React.createElement("canvas", { id: "myCanvas", onTouchStart: this.touchStart, onTouchMove: this.touchMove, onTouchEnd: this.touchEnd })), this.state.isArea2Show && /*#__PURE__*/React.createElement("div", { className: "area2", id: "area2" }, this.props.children)); } }]); return Scratch; }(React.PureComponent); export { Scratch as default }; Scratch.defaultProps = { prefixCls: 'Yep-scratch', style: {}, scratchImgUrl: 'http://img12.360buyimg.com/uba/jfs/t20377/335/194083123/261/85331804/5b02899bN0571f2a7.png', resCallback: function resCallback() {} };