Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow custom ratio/width/height in fromDataURL #253

Merged
merged 2 commits into from
Jun 11, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
## Changelog

### master

* Allow custom ratio/width/height when loading data URL onto canvas

### 2.1.1
* Fixed a bug where default value was applied for throttle when throttle was set to 0. ([mkrause](https://github.com/mkrause) in [#247](https://github.com/szimek/signature_pad/pull/247))

Expand Down
9 changes: 5 additions & 4 deletions src/signature_pad.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,12 @@ SignaturePad.prototype.clear = function () {
this._isEmpty = true;
};

SignaturePad.prototype.fromDataURL = function (dataUrl) {
SignaturePad.prototype.fromDataURL = function (dataUrl, options) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After rewrite to ES6, you can probably set the default values for function parameters, so you could write it as

function (dataUrl, options = {}) {...}

and get rid of opts variable.

Copy link
Contributor Author

@halo halo May 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. I wasn't sure so I followed the pattern i saw in other functions.

I changed it now, this is what it compiles to:

SignaturePad.prototype.fromDataURL = function (dataUrl) {
  var _this = this;

  var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};

  var image = new Image();
  var ratio = options.ratio || window.devicePixelRatio || 1;
  var width = options.width || this._canvas.width / ratio;
  var height = options.height || this._canvas.height / ratio;
  // ...

const image = new Image();
const ratio = window.devicePixelRatio || 1;
const width = this._canvas.width / ratio;
const height = this._canvas.height / ratio;
const opts = options || {};
const ratio = opts.ratio || window.devicePixelRatio || 1;
const width = opts.width || (this._canvas.width / ratio);
const height = opts.height || (this._canvas.height / ratio);

this._reset();
image.src = dataUrl;
Expand Down