Skip to content

Commit

Permalink
chore: add maxWidth option
Browse files Browse the repository at this point in the history
  • Loading branch information
semdy committed Aug 30, 2023
1 parent d1a79b2 commit f245ff9
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ new Signature.default({
minWidth: 2, // minimize linWidth
minSpeed: 1.5, // minimize brush move speed
scaleRatio: window.devicePixelRatio, // canvas scale ratio
maxWidth: null, // canvas element style max width
maxWidthDiffRate: 20, // Smooth transition threshold
resizeDebounceTime: 200, // window resize debounce elapse time
maxHistoryLength: 0, // max history length, no limit if set to 0
Expand Down
2 changes: 1 addition & 1 deletion demo/index.js

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions es/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ var Common = /*#__PURE__*/function () {
var _this$options = this.options,
width = _this$options.width,
height = _this$options.height,
maxWidth = _this$options.maxWidth,
root = _this$options.root,
scaleRatio = _this$options.scaleRatio;
var eWidth, eHeight;
Expand All @@ -299,6 +300,12 @@ var Common = /*#__PURE__*/function () {
this.drawElement.height = eHeight;
}

if (maxWidth) {
var origWidth = eWidth;
eWidth = Math.min(eWidth, maxWidth);
eHeight = Math.min(eHeight, eWidth / origWidth * eHeight);
}

this.drawElement.style.cssText = "width: ".concat(eWidth, "px; height: ").concat(eHeight, "px; touch-action: none;");

if (this.tempImageData) {
Expand Down Expand Up @@ -1201,6 +1208,7 @@ Stage.defaultOptions = {
minWidth: 2,
minSpeed: 1.5,
scaleRatio: window.devicePixelRatio || 1,
maxWidth: null,
maxWidthDiffRate: 20,
resizeDebounceTime: 200,
maxHistoryLength: 0,
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface IOptions {
minWidth?: number
minSpeed?: number
scaleRatio?: number
maxWidth?: number
maxWidthDiffRate?: number
resizeDebounceTime?: number
maxHistoryLength?: number
Expand Down
8 changes: 8 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ var Common = /*#__PURE__*/function () {
var _this$options = this.options,
width = _this$options.width,
height = _this$options.height,
maxWidth = _this$options.maxWidth,
root = _this$options.root,
scaleRatio = _this$options.scaleRatio;
var eWidth, eHeight;
Expand All @@ -323,6 +324,12 @@ var Common = /*#__PURE__*/function () {
this.drawElement.height = eHeight;
}

if (maxWidth) {
var origWidth = eWidth;
eWidth = Math.min(eWidth, maxWidth);
eHeight = Math.min(eHeight, eWidth / origWidth * eHeight);
}

this.drawElement.style.cssText = "width: ".concat(eWidth, "px; height: ").concat(eHeight, "px; touch-action: none;");

if (this.tempImageData) {
Expand Down Expand Up @@ -1225,6 +1232,7 @@ Stage.defaultOptions = {
minWidth: 2,
minSpeed: 1.5,
scaleRatio: window.devicePixelRatio || 1,
maxWidth: null,
maxWidthDiffRate: 20,
resizeDebounceTime: 200,
maxHistoryLength: 0,
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.3.2",
"version": "0.3.3",
"description": "h5 signature",
"main": "lib/index.js",
"module": "es/index.js",
Expand Down
7 changes: 6 additions & 1 deletion src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Common {
}

resize() {
const { width, height, root, scaleRatio } = this.options;
const { width, height, maxWidth, root, scaleRatio } = this.options;
let eWidth, eHeight;
if (width === "auto") {
eWidth = root.clientWidth;
Expand All @@ -28,6 +28,11 @@ class Common {
this.drawElement.width = eWidth;
this.drawElement.height = eHeight;
}
if (maxWidth) {
const origWidth = eWidth;
eWidth = Math.min(eWidth, maxWidth);
eHeight = Math.min(eHeight, (eWidth / origWidth) * eHeight);
}
this.drawElement.style.cssText = `width: ${eWidth}px; height: ${eHeight}px; touch-action: none;`;
if (this.tempImageData) {
this.drawCtx.putImageData(this.tempImageData, 0, 0);
Expand Down
1 change: 1 addition & 0 deletions src/stage.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ Stage.defaultOptions = {
minWidth: 2,
minSpeed: 1.5,
scaleRatio: window.devicePixelRatio || 1,
maxWidth: null,
maxWidthDiffRate: 20,
resizeDebounceTime: 200,
maxHistoryLength: 0,
Expand Down

0 comments on commit f245ff9

Please sign in to comment.