react-canvas-draw
Advanced tools
Comparing version 0.1.5 to 0.1.6
@@ -30,3 +30,3 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
_this.loadSaveData = function (saveData) { | ||
_this.loadSaveData = function (saveData, immediate) { | ||
try { | ||
@@ -69,9 +69,3 @@ if (typeof saveData !== "string") { | ||
_this.linesArray.forEach(function (line, idx) { | ||
// draw the line with a time offset | ||
// creates the cool drawing-animation effect | ||
window.setTimeout(function () { | ||
return _this.drawLine(line); | ||
}, idx * _this.props.loadTimeOffset); | ||
}); | ||
_this.redraw(immediate); | ||
} catch (err) { | ||
@@ -82,2 +76,21 @@ throw err; | ||
_this.redraw = function (immediate) { | ||
if (_this.ctx) { | ||
_this.ctx.clearRect(0, 0, _this.props.canvasWidth, _this.props.canvasHeight); | ||
} | ||
_this.linesArray.forEach(function (line, idx) { | ||
// draw the line with a time offset | ||
// creates the cool drawing-animation effect | ||
if (!immediate) { | ||
window.setTimeout(function () { | ||
return _this.drawLine(line); | ||
}, idx * _this.props.loadTimeOffset); | ||
} else { | ||
// if the immediate flag is true, draw without timeout | ||
_this.drawLine(line); | ||
} | ||
}); | ||
}; | ||
_this.getMousePos = function (e) { | ||
@@ -110,2 +123,11 @@ var rect = _this.canvas.getBoundingClientRect(); | ||
_this.undo = function () { | ||
if (_this.startDrawIdx.length > 0) { | ||
_this.linesArray.splice(_this.startDrawIdx.pop()); | ||
_this.redraw(true); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
_this.drawLine = function (line) { | ||
@@ -125,2 +147,4 @@ if (!_this.ctx) return; | ||
_this.isMouseDown = true; | ||
_this.startDrawIdx.push(_this.linesArray.length); | ||
console.log(_this.startDrawIdx); | ||
@@ -185,2 +209,3 @@ var _this$getMousePos = _this.getMousePos(e), | ||
_this.linesArray = []; | ||
_this.startDrawIdx = []; | ||
return _this; | ||
@@ -187,0 +212,0 @@ } |
@@ -39,3 +39,3 @@ "use strict"; | ||
_this.loadSaveData = function (saveData) { | ||
_this.loadSaveData = function (saveData, immediate) { | ||
try { | ||
@@ -78,9 +78,3 @@ if (typeof saveData !== "string") { | ||
_this.linesArray.forEach(function (line, idx) { | ||
// draw the line with a time offset | ||
// creates the cool drawing-animation effect | ||
window.setTimeout(function () { | ||
return _this.drawLine(line); | ||
}, idx * _this.props.loadTimeOffset); | ||
}); | ||
_this.redraw(immediate); | ||
} catch (err) { | ||
@@ -91,2 +85,21 @@ throw err; | ||
_this.redraw = function (immediate) { | ||
if (_this.ctx) { | ||
_this.ctx.clearRect(0, 0, _this.props.canvasWidth, _this.props.canvasHeight); | ||
} | ||
_this.linesArray.forEach(function (line, idx) { | ||
// draw the line with a time offset | ||
// creates the cool drawing-animation effect | ||
if (!immediate) { | ||
window.setTimeout(function () { | ||
return _this.drawLine(line); | ||
}, idx * _this.props.loadTimeOffset); | ||
} else { | ||
// if the immediate flag is true, draw without timeout | ||
_this.drawLine(line); | ||
} | ||
}); | ||
}; | ||
_this.getMousePos = function (e) { | ||
@@ -119,2 +132,11 @@ var rect = _this.canvas.getBoundingClientRect(); | ||
_this.undo = function () { | ||
if (_this.startDrawIdx.length > 0) { | ||
_this.linesArray.splice(_this.startDrawIdx.pop()); | ||
_this.redraw(true); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
_this.drawLine = function (line) { | ||
@@ -134,2 +156,4 @@ if (!_this.ctx) return; | ||
_this.isMouseDown = true; | ||
_this.startDrawIdx.push(_this.linesArray.length); | ||
console.log(_this.startDrawIdx); | ||
@@ -194,2 +218,3 @@ var _this$getMousePos = _this.getMousePos(e), | ||
_this.linesArray = []; | ||
_this.startDrawIdx = []; | ||
return _this; | ||
@@ -196,0 +221,0 @@ } |
{ | ||
"name": "react-canvas-draw", | ||
"version": "0.1.5", | ||
"version": "0.1.6", | ||
"description": "A simple yet powerful canvas-drawing component for React.", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -54,2 +54,23 @@ # React Canvas Draw | ||
### Functions | ||
Useful functions that you can call, e.g. when having a reference to this component: | ||
* `getSaveData()` returns the drawing's save-data as stringified JSON | ||
* `loadSaveData(saveData: String, immediate: Boolean)` loads a previously saved drawing using the saveData string, as well as an optional boolean flag to load it immediately, instead of live-drawing it. | ||
* `clear()` clears the canvas completely | ||
* `undo()` removes the latest change to the drawing. This includes everything drawn since the last MouseDown event. | ||
* `drawLine(line)` to draw a line. This can be useful if you want to automate drawing. The line parameter is an object of the following form: | ||
``` | ||
const line = { | ||
color: this.props.brushColor, | ||
size: this.props.brushSize, | ||
startX: this.x, | ||
startY: this.y, | ||
endX: newX, | ||
endY: newY | ||
}; | ||
``` | ||
## Local Development | ||
@@ -56,0 +77,0 @@ |
@@ -125,3 +125,3 @@ /*! | ||
_this.loadSaveData = function (saveData) { | ||
_this.loadSaveData = function (saveData, immediate) { | ||
try { | ||
@@ -164,9 +164,3 @@ if (typeof saveData !== "string") { | ||
_this.linesArray.forEach(function (line, idx) { | ||
// draw the line with a time offset | ||
// creates the cool drawing-animation effect | ||
window.setTimeout(function () { | ||
return _this.drawLine(line); | ||
}, idx * _this.props.loadTimeOffset); | ||
}); | ||
_this.redraw(immediate); | ||
} catch (err) { | ||
@@ -177,2 +171,21 @@ throw err; | ||
_this.redraw = function (immediate) { | ||
if (_this.ctx) { | ||
_this.ctx.clearRect(0, 0, _this.props.canvasWidth, _this.props.canvasHeight); | ||
} | ||
_this.linesArray.forEach(function (line, idx) { | ||
// draw the line with a time offset | ||
// creates the cool drawing-animation effect | ||
if (!immediate) { | ||
window.setTimeout(function () { | ||
return _this.drawLine(line); | ||
}, idx * _this.props.loadTimeOffset); | ||
} else { | ||
// if the immediate flag is true, draw without timeout | ||
_this.drawLine(line); | ||
} | ||
}); | ||
}; | ||
_this.getMousePos = function (e) { | ||
@@ -205,2 +218,11 @@ var rect = _this.canvas.getBoundingClientRect(); | ||
_this.undo = function () { | ||
if (_this.startDrawIdx.length > 0) { | ||
_this.linesArray.splice(_this.startDrawIdx.pop()); | ||
_this.redraw(true); | ||
return true; | ||
} | ||
return false; | ||
}; | ||
_this.drawLine = function (line) { | ||
@@ -220,2 +242,4 @@ if (!_this.ctx) return; | ||
_this.isMouseDown = true; | ||
_this.startDrawIdx.push(_this.linesArray.length); | ||
console.log(_this.startDrawIdx); | ||
@@ -280,2 +304,3 @@ var _this$getMousePos = _this.getMousePos(e), | ||
_this.linesArray = []; | ||
_this.startDrawIdx = []; | ||
return _this; | ||
@@ -282,0 +307,0 @@ } |
@@ -5,3 +5,3 @@ /*! | ||
*/ | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("react")):"function"==typeof define&&define.amd?define(["react"],e):"object"==typeof exports?exports.ReactCanvasDraw=e(require("react")):t.ReactCanvasDraw=e(t.React)}("undefined"!=typeof self?self:this,function(t){return function(t){var e={};function n(r){if(e[r])return e[r].exports;var o=e[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=t,n.c=e,n.d=function(t,e,r){n.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:r})},n.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(e,"a",e),e},n.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},n.p="",n(n.s=0)}([function(t,e,n){t.exports=n(1)},function(t,e,n){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),n.d(e,"default",function(){return c});var r,o,a=n(2),s=n.n(a),i=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t};var c=(o=r=function(t){function e(n){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,e);var r=function(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}(this,t.call(this,n));return r.getSaveData=function(){var t={linesArray:r.linesArray,width:r.props.canvasWidth,height:r.props.canvasHeight};return JSON.stringify(t)},r.loadSaveData=function(t){try{if("string"!=typeof t)throw new Error("saveData needs to be a stringified array!");var e=JSON.parse(t),n=e.linesArray,o=e.width,a=e.height;if(!n||"function"!=typeof n.push)throw new Error("linesArray needs to be an array!");if(r.clear(),o===r.props.canvasWidth&&a===r.props.canvasHeight)r.linesArray=n;else{var s=r.props.canvasWidth/o,c=r.props.canvasHeight/a,u=(s+c)/2;r.linesArray=n.map(function(t){return i({},t,{endX:t.endX*s,endY:t.endY*c,startX:t.startX*s,startY:t.startY*c,size:t.size*u})})}r.linesArray.forEach(function(t,e){window.setTimeout(function(){return r.drawLine(t)},e*r.props.loadTimeOffset)})}catch(t){throw t}},r.getMousePos=function(t){var e=r.canvas.getBoundingClientRect(),n=t.clientX,o=t.clientY;return t.touches&&t.touches.length>0&&(n=t.touches[0].clientX,o=t.touches[0].clientY),{x:n-e.left,y:o-e.top}},r.clear=function(){r.ctx&&r.ctx.clearRect(0,0,r.props.canvasWidth,r.props.canvasHeight),r.linesArray=[]},r.drawLine=function(t){r.ctx&&(r.ctx.strokeStyle=t.color,r.ctx.lineWidth=t.size,r.ctx.lineCap="round",r.ctx.beginPath(),r.ctx.moveTo(t.startX,t.startY),r.ctx.lineTo(t.endX,t.endY),r.ctx.stroke())},r.drawStart=function(t){r.isMouseDown=!0;var e=r.getMousePos(t),n=e.x,o=e.y;r.x=n,r.y=o,r.draw(t)},r.drawEnd=function(){r.isMouseDown=!1},r.draw=function(t){if(r.isMouseDown){var e=r.getMousePos(t),n=e.x+1,o=e.y+1,a={color:r.props.brushColor,size:r.props.brushSize,startX:r.x,startY:r.y,endX:n,endY:o};r.drawLine(a),r.linesArray.push(a),"function"==typeof r.props.onChange&&r.props.onChange(r.linesArray),r.x=n,r.y=o}},r.isMouseDown=!1,r.linesArray=[],r}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}(e,t),e.prototype.render=function(){var t=this;return s.a.createElement("canvas",{width:this.props.canvasWidth,height:this.props.canvasHeight,style:i({display:"block",background:"#fff",touchAction:"none"},this.props.style),ref:function(e){e&&(t.canvas=e,t.ctx=e.getContext("2d"))},onMouseDown:this.drawStart,onClick:function(){return!1},onMouseUp:this.drawEnd,onMouseOut:this.drawEnd,onMouseMove:this.draw,onTouchStart:this.drawStart,onTouchMove:this.draw,onTouchEnd:this.drawEnd,onTouchCancel:this.drawEnd})},e}(a.Component),r.defaultProps={loadTimeOffset:5,brushSize:6,brushColor:"#444",canvasWidth:400,canvasHeight:400},o)},function(e,n){e.exports=t}]).default}); | ||
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("react")):"function"==typeof define&&define.amd?define(["react"],e):"object"==typeof exports?exports.ReactCanvasDraw=e(require("react")):t.ReactCanvasDraw=e(t.React)}("undefined"!=typeof self?self:this,function(t){return function(t){var e={};function r(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{configurable:!1,enumerable:!0,get:n})},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=0)}([function(t,e,r){t.exports=r(1)},function(t,e,r){"use strict";Object.defineProperty(e,"__esModule",{value:!0}),r.d(e,"default",function(){return c});var n,o,a=r(2),s=r.n(a),i=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t};var c=(o=n=function(t){function e(r){!function(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}(this,e);var n=function(t,e){if(!t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!e||"object"!=typeof e&&"function"!=typeof e?t:e}(this,t.call(this,r));return n.getSaveData=function(){var t={linesArray:n.linesArray,width:n.props.canvasWidth,height:n.props.canvasHeight};return JSON.stringify(t)},n.loadSaveData=function(t,e){try{if("string"!=typeof t)throw new Error("saveData needs to be a stringified array!");var r=JSON.parse(t),o=r.linesArray,a=r.width,s=r.height;if(!o||"function"!=typeof o.push)throw new Error("linesArray needs to be an array!");if(n.clear(),a===n.props.canvasWidth&&s===n.props.canvasHeight)n.linesArray=o;else{var c=n.props.canvasWidth/a,u=n.props.canvasHeight/s,p=(c+u)/2;n.linesArray=o.map(function(t){return i({},t,{endX:t.endX*c,endY:t.endY*u,startX:t.startX*c,startY:t.startY*u,size:t.size*p})})}n.redraw(e)}catch(t){throw t}},n.redraw=function(t){n.ctx&&n.ctx.clearRect(0,0,n.props.canvasWidth,n.props.canvasHeight),n.linesArray.forEach(function(e,r){t?n.drawLine(e):window.setTimeout(function(){return n.drawLine(e)},r*n.props.loadTimeOffset)})},n.getMousePos=function(t){var e=n.canvas.getBoundingClientRect(),r=t.clientX,o=t.clientY;return t.touches&&t.touches.length>0&&(r=t.touches[0].clientX,o=t.touches[0].clientY),{x:r-e.left,y:o-e.top}},n.clear=function(){n.ctx&&n.ctx.clearRect(0,0,n.props.canvasWidth,n.props.canvasHeight),n.linesArray=[]},n.undo=function(){return n.startDrawIdx.length>0&&(n.linesArray.splice(n.startDrawIdx.pop()),n.redraw(!0),!0)},n.drawLine=function(t){n.ctx&&(n.ctx.strokeStyle=t.color,n.ctx.lineWidth=t.size,n.ctx.lineCap="round",n.ctx.beginPath(),n.ctx.moveTo(t.startX,t.startY),n.ctx.lineTo(t.endX,t.endY),n.ctx.stroke())},n.drawStart=function(t){n.isMouseDown=!0,n.startDrawIdx.push(n.linesArray.length),console.log(n.startDrawIdx);var e=n.getMousePos(t),r=e.x,o=e.y;n.x=r,n.y=o,n.draw(t)},n.drawEnd=function(){n.isMouseDown=!1},n.draw=function(t){if(n.isMouseDown){var e=n.getMousePos(t),r=e.x+1,o=e.y+1,a={color:n.props.brushColor,size:n.props.brushSize,startX:n.x,startY:n.y,endX:r,endY:o};n.drawLine(a),n.linesArray.push(a),"function"==typeof n.props.onChange&&n.props.onChange(n.linesArray),n.x=r,n.y=o}},n.isMouseDown=!1,n.linesArray=[],n.startDrawIdx=[],n}return function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(Object.setPrototypeOf?Object.setPrototypeOf(t,e):t.__proto__=e)}(e,t),e.prototype.render=function(){var t=this;return s.a.createElement("canvas",{width:this.props.canvasWidth,height:this.props.canvasHeight,style:i({display:"block",background:"#fff",touchAction:"none"},this.props.style),ref:function(e){e&&(t.canvas=e,t.ctx=e.getContext("2d"))},onMouseDown:this.drawStart,onClick:function(){return!1},onMouseUp:this.drawEnd,onMouseOut:this.drawEnd,onMouseMove:this.draw,onTouchStart:this.drawStart,onTouchMove:this.draw,onTouchEnd:this.drawEnd,onTouchCancel:this.drawEnd})},e}(a.Component),n.defaultProps={loadTimeOffset:5,brushSize:6,brushColor:"#444",canvasWidth:400,canvasHeight:400},o)},function(e,r){e.exports=t}]).default}); | ||
//# sourceMappingURL=react-canvas-draw.min.js.map |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52102
727
95