Topic: Perlin Noise + Canvas |
|
---|---|
Author | Thread |
Nervous Wreck (II) Inmate From: |
posted 05-01-2009 19:48
Here's a little function that fills a canvas with Perlin noise. It's not rocket science, but maybe someone will find it useful in their tinkering. code: function perlin_noise (canvas) { var canvas_ctx = canvas.getContext ("2d"), offscreen = document.createElement ("canvas"), offscreen_ctx = offscreen.getContext ("2d"), saved_alpha = canvas_ctx.globalAlpha /* Fill the offscreen buffer with random noise. */ offscreen.width = canvas.width offscreen.height = canvas.height var offscreen_id = offscreen_ctx.getImageData (0, 0, offscreen.width, offscreen.height), offscreen_pixels = offscreen_id.data for (var i = 0; i < offscreen_pixels.length; i += 4) { offscreen_pixels[i ] = offscreen_pixels[i + 1] = offscreen_pixels[i + 2] = Math.floor (Math.random () * 256) offscreen_pixels[i + 3] = 255 } offscreen_ctx.putImageData (offscreen_id, 0, 0) /* Scale random iterations onto the canvas to generate Perlin noise. */ for (var size = 4; size <= offscreen.width; size *= 2) { var x = Math.floor (Math.random () * (offscreen.width - size)), y = Math.floor (Math.random () * (offscreen.height - size)) canvas_ctx.globalAlpha = 4 / size canvas_ctx.drawImage (offscreen, x, y, size, size, 0, 0, canvas.width, canvas.height) } canvas_ctx.globalAlpha = saved_alpha } |
Paranoid (IV) Inmate From: Norway |
posted 05-02-2009 14:32 |
Nervous Wreck (II) Inmate From: Portland Oregon |
posted 05-09-2009 15:17
that's awesome. I had to play with it. |
Nervous Wreck (II) Inmate From: |
posted 05-12-2009 04:39
Bear in mind that all (canvas-based) images have 3 color channels: red, green, and blue. If you modify each of these color channels separately (like in that app you cite), you can get different color ratios. Bright Red + Bright Green = Bright Yellow, Dark Green + Bright Blue = Aqua, etc. |
Nervous Wreck (II) Inmate From: Portland Oregon |
posted 05-12-2009 04:50
You read my mind. I've thinking about how to do that. I was trying to follow the pixastic script with the histograms, but I just get confused with jQuery stuff and I had to give up. I would love to see how to do it. |
Nervous Wreck (II) Inmate From: Portland Oregon |
posted 05-12-2009 06:17
I saved this script from somewhere and I found a way to make it work on a local machine so it doesn't throw a security error. code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Canvas Primer - Example: ImageData colour inversion filter</title> <script type="text/javascript"> function runcolors(colorNum) { var elem = document.getElementById('myCanvas'); // Get the canvas 2d context. var context = elem.getContext('2d'); // Create a new image. var img = new Image(); img.src = 'logoBig.jpg'; elem.width=img.width elem.height=img.height // Once it's loaded draw the image on canvas and invert the colors. img.addEventListener('load', function () { var x = 0, y = 0; // Draw the image on canvas. context.drawImage(this, x, y,this.width,this.height,x,y,this.width,this.height); // Get the pixels. try { try { var imgd = context.getImageData(x, y, img.width, img.height); } catch (e) { netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); var imgd = context.getImageData(x, y, img.width, img.height); } } catch (e) { throw new Error("unable to access image data: " + e); } var pix = imgd.data; // Loop over each pixel and invert the color. for (var i = 0, n = pix.length; i < n; i += 4) { pix[i ] = 255 - pix[i ]; // red pix[i+1] = 255 - pix[i+1]*colorNum; // green pix[i+2] = 255 - pix[i+2]; // blue // i+3 is alpha (the fourth element) } // Draw the ImageData object. context.putImageData(imgd, x, y); }, false); } /*todo: make this function work. function rgb2lum( r, g, b ) { return( (0.299/255.0)*r + (0.587/255.0)*g + (0.114/255.0)*b ); } */ </script> </head> <body onload="runcolors(1)"> <canvas id="myCanvas" width="150" height="150" onmouseover="runcolors(.75)"></canvas> </body> </html> |
Nervous Wreck (II) Inmate From: |
posted 05-15-2009 23:17
Hey, sorry for the delay. Don't have all that much time in my life these days. I don't generally bother with jQuery or any of the other frameworks (I've been doing JS so long that it feels easier, if wordier, to do the scripts by hand--besides, I'm a total hacker anyways), so I'm afraid I can't help you much there. |
Nervous Wreck (II) Inmate From: Portland Oregon |
posted 05-16-2009 05:38
Very nice. Thanks for the examples. I'll be studying them. I really appreciate your help. |
Neurotic (0) Inmate Newly admitted From: |
posted 05-21-2010 12:29
Hi Iron Wallaby, I took your code and used it to make a cloudy sky |
Maniac (V) Mad Scientist From: 127 Halcyon Road, Marenia, Atlantis |
posted 05-21-2010 13:24
Procedural texture generation?! iron_wallaby, we need to talk! Do you know any C++? |
Obsessive-Compulsive (I) Inmate From: |
posted 07-30-2010 10:08
TP: Spam removed |
Nervous Wreck (II) Inmate From: |
posted 09-10-2010 02:00
Wow, long time since I've been here... |
Nervous Wreck (II) Inmate From: |
posted 05-31-2011 11:00
Edit TP: spam removed
|