MCPcopy Create free account
hub / github.com/DavidHDev/react-bits / generateProceduralTexture

Function generateProceduralTexture

src/tools/texture-lab/utils.js:238–319  ·  view source on GitHub ↗
(type, size = 256)

Source from the content-addressed store, hash-verified

236};
237
238export const generateProceduralTexture = (type, size = 256) => {
239 const canvas = document.createElement('canvas');
240 canvas.width = size;
241 canvas.height = size;
242 const ctx = canvas.getContext('2d');
243 const imageData = ctx.createImageData(size, size);
244 const data = imageData.data;
245
246 const seededRandom = seed => {
247 const x = Math.sin(seed) * 10000;
248 return x - Math.floor(x);
249 };
250
251 switch (type) {
252 case 'paper': {
253 for (let i = 0; i < data.length; i += 4) {
254 const px = (i / 4) % size;
255 const py = Math.floor(i / 4 / size);
256 const noise1 = seededRandom(px * 0.1 + py * 0.1);
257 const noise2 = seededRandom(px * 0.05 + py * 0.2 + 1000);
258 const value = Math.round((noise1 * 0.7 + noise2 * 0.3) * 60 + 195);
259 data[i] = data[i + 1] = data[i + 2] = value;
260 data[i + 3] = 255;
261 }
262 break;
263 }
264
265 case 'film-grain': {
266 for (let i = 0; i < data.length; i += 4) {
267 const noise = seededRandom(i);
268 const value = Math.round(noise * 255);
269 data[i] = data[i + 1] = data[i + 2] = value;
270 data[i + 3] = Math.round(noise * 100);
271 }
272 break;
273 }
274
275 case 'canvas': {
276 for (let i = 0; i < data.length; i += 4) {
277 const px = (i / 4) % size;
278 const py = Math.floor(i / 4 / size);
279 const weave = (Math.sin(px * 0.5) * 0.5 + 0.5) * (Math.sin(py * 0.5) * 0.5 + 0.5);
280 const noise = seededRandom(i) * 0.2;
281 const value = Math.round((weave + noise) * 60 + 180);
282 data[i] = data[i + 1] = data[i + 2] = value;
283 data[i + 3] = 255;
284 }
285 break;
286 }
287
288 case 'dust': {
289 for (let i = 0; i < data.length; i += 4) {
290 const noise = seededRandom(i);
291 if (noise > 0.98) {
292 data[i] = data[i + 1] = data[i + 2] = 0;
293 data[i + 3] = Math.round(seededRandom(i + 1) * 200);
294 } else if (noise > 0.95) {
295 data[i] = data[i + 1] = data[i + 2] = 255;

Callers 1

loadOverlayTexturesFunction · 0.90

Calls 1

seededRandomFunction · 0.85

Tested by

no test coverage detected