Skip to content

Instantly share code, notes, and snippets.

@BolajiOlajide
Created October 26, 2023 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BolajiOlajide/4da12eff0a6388a8c152bf3da33a0fe0 to your computer and use it in GitHub Desktop.
Save BolajiOlajide/4da12eff0a6388a8c152bf3da33a0fe0 to your computer and use it in GitHub Desktop.
svelte-paint-gradient.js
export function paint(context, t) {
const { width, height } = context.canvas;
const imageData = context.getImageData(0, 0, width, height);
for (let p = 0; p < imageData.data.length; p += 4) {
const i = p / 4;
const x = i % width;
const y = (i / width) >>> 0;
const red = 64 + (128 * x) / width + 64 * Math.sin(t / 1000);
const green = 64 + (128 * y) / height + 64 * Math.cos(t / 1000);
const blue = 128;
imageData.data[p + 0] = red;
imageData.data[p + 1] = green;
imageData.data[p + 2] = blue;
imageData.data[p + 3] = 255;
}
context.putImageData(imageData, 0, 0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment