Skip to content

Instantly share code, notes, and snippets.

@tripolskypetr
Last active January 2, 2020 21:45
Show Gist options
  • Save tripolskypetr/ddbedf26558f09021cdb2bf1cb7c7037 to your computer and use it in GitHub Desktop.
Save tripolskypetr/ddbedf26558f09021cdb2bf1cb7c7037 to your computer and use it in GitHub Desktop.
export default class ImageFactory {
static _index = 0;
static _colors = ["red", "orange", "yellow", "green", "skyblue", "blue", "cyan"];
static _increment() {
const self = ImageFactory;
return self._index = (++self._index > self._colors.length - 1) ? 0 : self._index;
}
static createImage(height = 100, width = 100) {
const canvas = document.createElement("canvas");
const context = canvas.getContext("2d");
const index = ImageFactory._increment();
[canvas.height, canvas.width] = [height, width];
context.fillStyle = ImageFactory._colors[index];
context.fillRect(0, 0, width, height);
return canvas.toDataURL("image/png");
};
constructor() {
throw new Error("ImageGenerator is static factory");
}
}
const appendImage = (isWild = false) => {
const image = document.createElement("img");
image.src = ImageFactory.createImage(250, 250 * (isWild ? 2 : 1));
document.body.appendChild(image);
}
/*
USAGE:
appendImage(true);
appendImage();
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment