Skip to content

Commit

Permalink
Merge pull request #6447 from Gaurav-1306/pixeldensity
Browse files Browse the repository at this point in the history
fixed the issue #6114
  • Loading branch information
limzykenneth authored Oct 12, 2023
2 parents efdaa35 + 27740b0 commit c5f5abb
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/core/p5.Renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,11 @@ class Renderer extends p5.Element {
// get(x,y,w,h)
}

const region = new p5.Image(w, h);
const region = new p5.Image(w*pd, h*pd);
region._pixelDensity = pd;
region.canvas
.getContext('2d')
.drawImage(canvas, x, y, w * pd, h * pd, 0, 0, w, h);
.drawImage(canvas, x, y, w * pd, h * pd, 0, 0, w*pd, h*pd);

return region;
}
Expand Down
42 changes: 42 additions & 0 deletions src/image/p5.Image.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,48 @@ p5.Image = class {
this.pixels = [];
}

/**
* Gets or sets the pixel density for high pixel density displays. By default,
* the density will be set to 1.
*
* Call this method with no arguments to get the default density, or pass
* in a number to set the density. If a non-positive number is provided,
* it defaults to 1.
*
* @method pixelDensity
* @param {Number} [density] A scaling factor for the number of pixels per
* side
* @returns {Number} The current density if called without arguments, or the instance for chaining if setting density.
*/
pixelDensity(density) {
if (typeof density !== 'undefined') {
// Setter: set the density and handle resize
if (density <= 0) {
const errorObj = {
type: 'INVALID_VALUE',
format: { types: ['Number'] },
position: 1
};

p5._friendlyParamError(errorObj, 'pixelDensity');

// Default to 1 in case of an invalid value
density = 1;
}

this._pixelDensity = density;

// Adjust canvas dimensions based on pixel density
this.width /= density;
this.height /= density;

return this; // Return the image instance for chaining if needed
} else {
// Getter: return the default density
return this._pixelDensity;
}
}

/**
* Helper function for animating GIF-based images with time
*/
Expand Down

0 comments on commit c5f5abb

Please sign in to comment.