Skip to content

Commit

Permalink
Removed unnecessary parameters to iconGenerator. Icon is always ren…
Browse files Browse the repository at this point in the history
…dered at position 0,0, and the size is known by the renderer.
  • Loading branch information
dmester committed Jul 31, 2020
1 parent f34eb7c commit 7a7ade9
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/apis/drawIcon.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ export function drawIcon(ctx, hashOrValue, size, config) {

iconGenerator(new CanvasRenderer(ctx, size),
isValidHash(hashOrValue) || computeHash(hashOrValue),
0, 0, size, getConfiguration(config, 0));
getConfiguration(config, 0));
}
2 changes: 1 addition & 1 deletion src/apis/toPng.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function toPng(hashOrValue, size, config) {

iconGenerator(new CanvasRenderer(ctx, size),
isValidHash(hashOrValue) || computeHash(hashOrValue),
0, 0, size, getConfiguration(config, 0.08));
getConfiguration(config, 0.08));

return canvas.toPng({ "Software": "Jdenticon" });
}
2 changes: 1 addition & 1 deletion src/apis/toSvg.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ export function toSvg(hashOrValue, size, config) {
const writer = new SvgWriter(size);
iconGenerator(new SvgRenderer(writer),
isValidHash(hashOrValue) || computeHash(hashOrValue),
0, 0, size, getConfiguration(config, 0.08));
getConfiguration(config, 0.08));
return writer.toString();
}
2 changes: 1 addition & 1 deletion src/apis/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,6 @@ function renderDomElement(el, hashOrValue, config, rendererFactory) {
const renderer = rendererFactory(el, getIdenticonType(el));
if (renderer) {
// Draw icon
iconGenerator(renderer, hash, 0, 0, renderer.size, getConfiguration(config, 0.08));
iconGenerator(renderer, hash, getConfiguration(config, 0.08));
}
}
18 changes: 8 additions & 10 deletions src/renderer/canvas/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,18 @@ export class CanvasRenderer {

ctx.save();

this._ctx = ctx;

if (size) {
this.size = size;
}
else {
this.size = Math.min(width, height);
if (!size) {
size = Math.min(width, height);

ctx.translate(
((width - this.size) / 2) | 0,
((height - this.size) / 2) | 0);
((width - size) / 2) | 0,
((height - size) / 2) | 0);
}

this._ctx = ctx;
this.size = size;

ctx.clearRect(0, 0, this.size, this.size);
ctx.clearRect(0, 0, size, size);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/renderer/iconGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import { parseHex } from "../common/parseHex";
/**
* Draws an identicon to a specified renderer.
*/
export function iconGenerator(renderer, hash, x, y, size, config) {
export function iconGenerator(renderer, hash, config) {
// Set background color
if (config.backColor) {
renderer.setBackground(config.backColor);
}

// Calculate padding and round to nearest integer
let size = renderer.size;
const padding = (0.5 + size * config.padding) | 0;
size -= padding * 2;

Expand All @@ -29,8 +30,8 @@ export function iconGenerator(renderer, hash, x, y, size, config) {
const cell = 0 | (size / 4);

// Since the cell size is integer based, the actual icon will be slightly smaller than specified => center icon
x += 0 | (padding + size / 2 - cell * 2);
y += 0 | (padding + size / 2 - cell * 2);
const x = 0 | (padding + size / 2 - cell * 2);
const y = 0 | (padding + size / 2 - cell * 2);

function renderShape(colorIndex, shapes, index, rotationIndex, positions) {
const shape = shapes[parseHex(hash, index, 1) % shapes.length];
Expand Down

0 comments on commit 7a7ade9

Please sign in to comment.