Created
October 13, 2020 13:58
-
-
Save PavelLaptev/cd1f31969824ca49387a2f85d8d393c7 to your computer and use it in GitHub Desktop.
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 const generateStripeTexture = ( | |
text, | |
colors = { main: "#ffa1a1", second: "blue" } | |
) => { | |
const copyAmount = 2; | |
const canvasSize = 640; | |
const fontSize = canvasSize / copyAmount; | |
const fontStyle = `Bold ${fontSize}px Arial`; | |
const bitmap = document.createElement("canvas"); | |
const g = bitmap.getContext("2d"); | |
g.font = fontStyle; | |
bitmap.width = g.measureText(text).width; | |
bitmap.height = fontSize * 2; | |
const generateTextRow = (shift, i) => { | |
// background | |
g.fillStyle = Object.values(colors)[i]; | |
g.fillRect(0, shift * i, bitmap.width, bitmap.height); | |
// text | |
g.font = `Bold ${fontSize}px Arial`; | |
// g.fillStyle = Object.values(colors)[i]; | |
g.fillText(text, 0, fontSize * i - 40); | |
g.fillStyle = Object.values(colors)[0]; | |
}; | |
Array(copyAmount + 1) | |
.fill(0) | |
.forEach((item, i) => { | |
generateTextRow(bitmap.height / 2, i); | |
}); | |
// text | |
return bitmap; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment