Last active
January 2, 2020 21:45
-
-
Save tripolskypetr/ddbedf26558f09021cdb2bf1cb7c7037 to your computer and use it in GitHub Desktop.
JS lorem image https://jsfiddle.net/tripolskypetr/0ydt3xmk/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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