Skip to content

Commit

Permalink
perf: catch error throw when hot reload
Browse files Browse the repository at this point in the history
  • Loading branch information
semdy committed Jul 26, 2023
1 parent a0fb903 commit 8da1788
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 125 deletions.
2 changes: 1 addition & 1 deletion demo/index.js

Large diffs are not rendered by default.

91 changes: 48 additions & 43 deletions es/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,7 @@ var Painter = /*#__PURE__*/function (_Base) {
}, {
key: "clear",
value: function clear() {
if (!this.drawElement) return;
this.drawCtx.clearRect(0, 0, this.drawElement.width, this.drawElement.height);
}
}, {
Expand Down Expand Up @@ -1008,62 +1009,66 @@ var Stage = /*#__PURE__*/function () {
}, {
key: "getResult",
value: function getResult(origin) {
var canvas = this.drawElement;
try {
var canvas = this.drawElement;

if (this.options.rotate !== 0) {
canvas = this.getRotateCanvas(this.options.rotate);
}
if (this.options.rotate !== 0) {
canvas = this.getRotateCanvas(this.options.rotate);
}

var _this$getValidBound = this.getValidBound(canvas),
cutWidth = _this$getValidBound.cutWidth,
cutHeight = _this$getValidBound.cutHeight,
lOffset = _this$getValidBound.lOffset,
tOffset = _this$getValidBound.tOffset;
var _this$getValidBound = this.getValidBound(canvas),
cutWidth = _this$getValidBound.cutWidth,
cutHeight = _this$getValidBound.cutHeight,
lOffset = _this$getValidBound.lOffset,
tOffset = _this$getValidBound.tOffset;

if (cutWidth <= 0 || cutHeight <= 0) return;
if (cutWidth <= 0 || cutHeight <= 0) return;

if (origin) {
return canvas;
}
if (origin) {
return canvas;
}

var _this$options = this.options,
exportMaxWidth = _this$options.exportMaxWidth,
exportMaxHeight = _this$options.exportMaxHeight,
exportPadding = _this$options.exportPadding,
scaleRatio = _this$options.scaleRatio;
var cutCanvas = document.createElement('canvas');
var cutCtx = cutCanvas.getContext('2d');
cutCanvas.width = cutWidth;
cutCanvas.height = cutHeight;
cutCtx.drawImage(canvas, lOffset, tOffset, cutWidth, cutHeight, 0, 0, cutCanvas.width, cutCanvas.height);
var _this$options = this.options,
exportMaxWidth = _this$options.exportMaxWidth,
exportMaxHeight = _this$options.exportMaxHeight,
exportPadding = _this$options.exportPadding,
scaleRatio = _this$options.scaleRatio;
var cutCanvas = document.createElement('canvas');
var cutCtx = cutCanvas.getContext('2d');
cutCanvas.width = cutWidth;
cutCanvas.height = cutHeight;
cutCtx.drawImage(canvas, lOffset, tOffset, cutWidth, cutHeight, 0, 0, cutCanvas.width, cutCanvas.height);

if (exportMaxWidth || exportMaxHeight || exportPadding !== 0) {
var exWidth = cutCanvas.width;
var exHeight = cutCanvas.height;
if (exportMaxWidth || exportMaxHeight || exportPadding !== 0) {
var exWidth = cutCanvas.width;
var exHeight = cutCanvas.height;

var _exportMaxWidth = exportMaxWidth * scaleRatio;
var _exportMaxWidth = exportMaxWidth * scaleRatio;

var _exportMaxHeight = exportMaxHeight * scaleRatio;
var _exportMaxHeight = exportMaxHeight * scaleRatio;

if (exportMaxWidth && _exportMaxWidth < exWidth) {
exHeight = exHeight * (_exportMaxWidth / exWidth);
exWidth = _exportMaxWidth;
}
if (exportMaxWidth && _exportMaxWidth < exWidth) {
exHeight = exHeight * (_exportMaxWidth / exWidth);
exWidth = _exportMaxWidth;
}

if (exportMaxHeight && _exportMaxHeight < exHeight) {
exWidth = exWidth * (_exportMaxHeight / exHeight);
exHeight = _exportMaxHeight;
if (exportMaxHeight && _exportMaxHeight < exHeight) {
exWidth = exWidth * (_exportMaxHeight / exHeight);
exHeight = _exportMaxHeight;
}

var exportCanvas = document.createElement('canvas');
var exportCtx = exportCanvas.getContext('2d');
exportCanvas.width = exWidth;
exportCanvas.height = exHeight;
exportCtx.drawImage(cutCanvas, exportPadding, exportPadding, exportCanvas.width - exportPadding * 2, exportCanvas.height - exportPadding * 2);
return exportCanvas;
}

var exportCanvas = document.createElement('canvas');
var exportCtx = exportCanvas.getContext('2d');
exportCanvas.width = exWidth;
exportCanvas.height = exHeight;
exportCtx.drawImage(cutCanvas, exportPadding, exportPadding, exportCanvas.width - exportPadding * 2, exportCanvas.height - exportPadding * 2);
return exportCanvas;
return cutCanvas;
} catch (e) {
console.error(e);
}

return cutCanvas;
}
}, {
key: "getRotateCanvas",
Expand Down
91 changes: 48 additions & 43 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ var Painter = /*#__PURE__*/function (_Base) {
}, {
key: "clear",
value: function clear() {
if (!this.drawElement) return;
this.drawCtx.clearRect(0, 0, this.drawElement.width, this.drawElement.height);
}
}, {
Expand Down Expand Up @@ -1032,62 +1033,66 @@ var Stage = /*#__PURE__*/function () {
}, {
key: "getResult",
value: function getResult(origin) {
var canvas = this.drawElement;
try {
var canvas = this.drawElement;

if (this.options.rotate !== 0) {
canvas = this.getRotateCanvas(this.options.rotate);
}
if (this.options.rotate !== 0) {
canvas = this.getRotateCanvas(this.options.rotate);
}

var _this$getValidBound = this.getValidBound(canvas),
cutWidth = _this$getValidBound.cutWidth,
cutHeight = _this$getValidBound.cutHeight,
lOffset = _this$getValidBound.lOffset,
tOffset = _this$getValidBound.tOffset;
var _this$getValidBound = this.getValidBound(canvas),
cutWidth = _this$getValidBound.cutWidth,
cutHeight = _this$getValidBound.cutHeight,
lOffset = _this$getValidBound.lOffset,
tOffset = _this$getValidBound.tOffset;

if (cutWidth <= 0 || cutHeight <= 0) return;
if (cutWidth <= 0 || cutHeight <= 0) return;

if (origin) {
return canvas;
}
if (origin) {
return canvas;
}

var _this$options = this.options,
exportMaxWidth = _this$options.exportMaxWidth,
exportMaxHeight = _this$options.exportMaxHeight,
exportPadding = _this$options.exportPadding,
scaleRatio = _this$options.scaleRatio;
var cutCanvas = document.createElement('canvas');
var cutCtx = cutCanvas.getContext('2d');
cutCanvas.width = cutWidth;
cutCanvas.height = cutHeight;
cutCtx.drawImage(canvas, lOffset, tOffset, cutWidth, cutHeight, 0, 0, cutCanvas.width, cutCanvas.height);
var _this$options = this.options,
exportMaxWidth = _this$options.exportMaxWidth,
exportMaxHeight = _this$options.exportMaxHeight,
exportPadding = _this$options.exportPadding,
scaleRatio = _this$options.scaleRatio;
var cutCanvas = document.createElement('canvas');
var cutCtx = cutCanvas.getContext('2d');
cutCanvas.width = cutWidth;
cutCanvas.height = cutHeight;
cutCtx.drawImage(canvas, lOffset, tOffset, cutWidth, cutHeight, 0, 0, cutCanvas.width, cutCanvas.height);

if (exportMaxWidth || exportMaxHeight || exportPadding !== 0) {
var exWidth = cutCanvas.width;
var exHeight = cutCanvas.height;
if (exportMaxWidth || exportMaxHeight || exportPadding !== 0) {
var exWidth = cutCanvas.width;
var exHeight = cutCanvas.height;

var _exportMaxWidth = exportMaxWidth * scaleRatio;
var _exportMaxWidth = exportMaxWidth * scaleRatio;

var _exportMaxHeight = exportMaxHeight * scaleRatio;
var _exportMaxHeight = exportMaxHeight * scaleRatio;

if (exportMaxWidth && _exportMaxWidth < exWidth) {
exHeight = exHeight * (_exportMaxWidth / exWidth);
exWidth = _exportMaxWidth;
}
if (exportMaxWidth && _exportMaxWidth < exWidth) {
exHeight = exHeight * (_exportMaxWidth / exWidth);
exWidth = _exportMaxWidth;
}

if (exportMaxHeight && _exportMaxHeight < exHeight) {
exWidth = exWidth * (_exportMaxHeight / exHeight);
exHeight = _exportMaxHeight;
if (exportMaxHeight && _exportMaxHeight < exHeight) {
exWidth = exWidth * (_exportMaxHeight / exHeight);
exHeight = _exportMaxHeight;
}

var exportCanvas = document.createElement('canvas');
var exportCtx = exportCanvas.getContext('2d');
exportCanvas.width = exWidth;
exportCanvas.height = exHeight;
exportCtx.drawImage(cutCanvas, exportPadding, exportPadding, exportCanvas.width - exportPadding * 2, exportCanvas.height - exportPadding * 2);
return exportCanvas;
}

var exportCanvas = document.createElement('canvas');
var exportCtx = exportCanvas.getContext('2d');
exportCanvas.width = exWidth;
exportCanvas.height = exHeight;
exportCtx.drawImage(cutCanvas, exportPadding, exportPadding, exportCanvas.width - exportPadding * 2, exportCanvas.height - exportPadding * 2);
return exportCanvas;
return cutCanvas;
} catch (e) {
console.error(e);
}

return cutCanvas;
}
}, {
key: "getRotateCanvas",
Expand Down
2 changes: 1 addition & 1 deletion lib/index.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "h5-signature",
"version": "0.2.8",
"version": "0.2.9",
"description": "h5 signature",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down
1 change: 1 addition & 0 deletions src/painter.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class Painter extends Base {
}

clear() {
if (!this.drawElement) return
this.drawCtx.clearRect(0, 0, this.drawElement.width, this.drawElement.height)
}

Expand Down
76 changes: 40 additions & 36 deletions src/stage.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,52 +146,56 @@ class Stage {
}

getResult(origin) {
let canvas = this.drawElement
if (this.options.rotate !== 0) {
canvas = this.getRotateCanvas(this.options.rotate)
}
try {
let canvas = this.drawElement
if (this.options.rotate !== 0) {
canvas = this.getRotateCanvas(this.options.rotate)
}

const { cutWidth, cutHeight, lOffset, tOffset } = this.getValidBound(canvas)
const { cutWidth, cutHeight, lOffset, tOffset } = this.getValidBound(canvas)

if (cutWidth <= 0 || cutHeight <= 0) return
if (cutWidth <= 0 || cutHeight <= 0) return

if (origin) {
return canvas
}
if (origin) {
return canvas
}

const { exportMaxWidth, exportMaxHeight, exportPadding, scaleRatio } = this.options
const { exportMaxWidth, exportMaxHeight, exportPadding, scaleRatio } = this.options

const cutCanvas = document.createElement('canvas')
const cutCtx = cutCanvas.getContext('2d')
cutCanvas.width = cutWidth
cutCanvas.height = cutHeight
cutCtx.drawImage(canvas, lOffset, tOffset, cutWidth, cutHeight, 0, 0, cutCanvas.width, cutCanvas.height)
const cutCanvas = document.createElement('canvas')
const cutCtx = cutCanvas.getContext('2d')
cutCanvas.width = cutWidth
cutCanvas.height = cutHeight
cutCtx.drawImage(canvas, lOffset, tOffset, cutWidth, cutHeight, 0, 0, cutCanvas.width, cutCanvas.height)

if (exportMaxWidth || exportMaxHeight || exportPadding !== 0) {
let exWidth = cutCanvas.width
let exHeight = cutCanvas.height
const _exportMaxWidth = exportMaxWidth * scaleRatio
const _exportMaxHeight = exportMaxHeight * scaleRatio
if (exportMaxWidth || exportMaxHeight || exportPadding !== 0) {
let exWidth = cutCanvas.width
let exHeight = cutCanvas.height
const _exportMaxWidth = exportMaxWidth * scaleRatio
const _exportMaxHeight = exportMaxHeight * scaleRatio

if (exportMaxWidth && _exportMaxWidth < exWidth) {
exHeight = exHeight * (_exportMaxWidth / exWidth)
exWidth = _exportMaxWidth
}
if (exportMaxHeight && _exportMaxHeight < exHeight) {
exWidth = exWidth * (_exportMaxHeight / exHeight)
exHeight = _exportMaxHeight
}
if (exportMaxWidth && _exportMaxWidth < exWidth) {
exHeight = exHeight * (_exportMaxWidth / exWidth)
exWidth = _exportMaxWidth
}
if (exportMaxHeight && _exportMaxHeight < exHeight) {
exWidth = exWidth * (_exportMaxHeight / exHeight)
exHeight = _exportMaxHeight
}

const exportCanvas = document.createElement('canvas')
const exportCtx = exportCanvas.getContext('2d')
exportCanvas.width = exWidth
exportCanvas.height = exHeight
exportCtx.drawImage(cutCanvas, exportPadding, exportPadding, exportCanvas.width - exportPadding * 2, exportCanvas.height - exportPadding * 2)
const exportCanvas = document.createElement('canvas')
const exportCtx = exportCanvas.getContext('2d')
exportCanvas.width = exWidth
exportCanvas.height = exHeight
exportCtx.drawImage(cutCanvas, exportPadding, exportPadding, exportCanvas.width - exportPadding * 2, exportCanvas.height - exportPadding * 2)

return exportCanvas
}
return exportCanvas
}

return cutCanvas
return cutCanvas
} catch(e) {
console.error(e)
}
}

getRotateCanvas(degree = 90) {
Expand Down

0 comments on commit 8da1788

Please sign in to comment.