<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ian M</title>
    <description>The latest articles on DEV Community by Ian M (@imac).</description>
    <link>https://dev.to/imac</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1353368%2F3ea4725d-dffa-43cb-86c7-85c2b7fc4e8b.png</url>
      <title>DEV Community: Ian M</title>
      <link>https://dev.to/imac</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/imac"/>
    <language>en</language>
    <item>
      <title>Image Effects With HTML Canvas</title>
      <dc:creator>Ian M</dc:creator>
      <pubDate>Fri, 03 May 2024 20:05:21 +0000</pubDate>
      <link>https://dev.to/imac/image-effects-with-html-canvas-1kgf</link>
      <guid>https://dev.to/imac/image-effects-with-html-canvas-1kgf</guid>
      <description>&lt;p&gt;HTML &lt;code&gt;Canvas&lt;/code&gt; is an element I don't see very often in the wild, but when I do, there's always something interesting going on there. In this guide, we will look at image manipulation inside the canvas element to produce some interesting effects that are fun to play with and can really up the wow factor on your websites.&lt;/p&gt;

&lt;p&gt;The element &lt;code&gt;&amp;lt;canvas&amp;gt;&lt;/code&gt; can be likened to an artist's canvas in that both start off blank give freedom to the artist to create any visual item they like. The possibilities are limitless - &lt;a href="https://skia.org/docs/user/modules/canvaskit/" rel="noopener noreferrer"&gt;Canvaskit&lt;/a&gt; is a mind-blowing example.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting up your development environment&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;All you need is a code editor, browser, and basic HTML/JS knowledge. &lt;br&gt;
Open a folder in your code editor and create an index.html file with some boilerplate.&lt;br&gt;
Add a &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag and a &lt;code&gt;&amp;lt;canvas&amp;gt;&lt;/code&gt; element with a class.&lt;/p&gt;

&lt;p&gt;Log something to the console to check that the script is working :)&lt;/p&gt;

&lt;p&gt;The canvas element is transparent by default and has no width /height set, so we'll add those and a background for clarity. &lt;br&gt;
Finally we separate the javascript from the rest and get to work.&lt;/p&gt;

&lt;p&gt;index.html&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html&amp;gt;
  &amp;lt;head&amp;gt;
    &amp;lt;title&amp;gt;&amp;lt;/title&amp;gt;
    &amp;lt;style&amp;gt;
        .myCanvas {
            width: 50vw;
            height: 50vw;
            background-color: black;
        }
    &amp;lt;/style&amp;gt;
    &amp;lt;script src="script.js"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;/head&amp;gt;
  &amp;lt;body&amp;gt;
    &amp;lt;canvas class="myCanvas"&amp;gt;&amp;lt;/canvas&amp;gt;
  &amp;lt;/body&amp;gt;
&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;script.js&lt;/p&gt;

&lt;p&gt;&lt;code&gt;console.log('hello from script')&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Open index.html in a browser. If you have a black square on your screen and a log on your console, we are definitely on the same page, pun intended XD.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2FiMac7%2FDraft%2Fassets%2F76876702%2F948bf68d-29aa-4b21-b58a-adaec60be838" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2FiMac7%2FDraft%2Fassets%2F76876702%2F948bf68d-29aa-4b21-b58a-adaec60be838" alt="Screenshot_20240403_155443"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Adjust the width and height of the canvas according to your preferences and let's start drawing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Still image effects&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Get an image of your liking and note its url. Paste the code below into your script.js or follow along as we go through it line by line.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('.myCanvas')

function rgbToGrayscale(r, g, b) {
    // Luminosity method, makes grayscale more accurate to humans
    return 0.21 * r + 0.72 * g + 0.07 * b;
}

function drawGrayscaleImage(imageUrl) {
    const img = new Image()

    img.onload = function() {
        // get whatever is on the face of the canvas
        const ctx = canvas.getContext('2d')

        // set canvas dimensions to match the image dimensions, prevent image distortion
        canvas.width = this.width
        canvas.height = this.height

        ctx.drawImage(this, 0, 0)

        // Get the pixel data of the image on the canvas
        const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
        const pixelData = imageData.data;

        // Loop through each pixel in the image data, 
        // replacing any color with grayscale value
        for (let i = 0; i &amp;lt; pixelData.length; i += 4) {
            const r = pixelData[i]
            const g = pixelData[i + 1]
            const b = pixelData[i + 2]

            const grayscaleValue = rgbToGrayscale(r, g, b)

            pixelData[i] = grayscaleValue
            pixelData[i + 1] = grayscaleValue
            pixelData[i + 2] = grayscaleValue
            pixelData[i + 3] = 255  // controls opacity
        }

        // Update the canvas with modified image data
        ctx.putImageData(imageData, 0, 0)
    }

    img.src = imageUrl
}

const imageUrl = 'my-image-url.jpg'

drawGrayscaleImage(imageUrl)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First we query the dom for the canvas element. &lt;/p&gt;

&lt;p&gt;Next create a new image with the url of your downloaded image and draw it on the canvas with &lt;code&gt;ctx.drawImage()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Get the drawn image's pixel data, the tiny pieces of color that make up an image, and somehow change it.&lt;br&gt;
Think of &lt;code&gt;canvas.getContext('2d')&lt;/code&gt; (in the code) as the 2D contents of the canvas, in this case an image. The 3D version is &lt;code&gt;canvas.getContext('webgl')&lt;/code&gt; which we won't cover for now.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ctx.drawImage()&lt;/code&gt; draws an image on the canvas.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ctx.getImageData()&lt;/code&gt; gets the pixel data of the image we just placed on the canvas.&lt;/p&gt;

&lt;p&gt;Loop through each pixel of the image, convert its RGB values to grayscale using the provided function &lt;code&gt;rgbToGrayscale&lt;/code&gt; which makes a new grayscale image.&lt;/p&gt;

&lt;p&gt;Draw the new image on the canvas.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2FiMac7%2FDraft%2Fassets%2F76876702%2F79e3a72d-ded6-447f-a643-7b72767458e8" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2FiMac7%2FDraft%2Fassets%2F76876702%2F79e3a72d-ded6-447f-a643-7b72767458e8" alt="Screenshot_20240403_155247"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Try playing with the code a bit, raise and lower alpha value in the image's pixel data, comment out or invert colors (255 - current_color) for different interesting results. &lt;/p&gt;

&lt;p&gt;We've seen how to apply colors on a still image, but how about animating it?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Particle image effects&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's create another javascript file called particles.js. We'll learn a bit of OOP along the way.&lt;br&gt;
Comment out the image.js script in &lt;code&gt;index.html&lt;/code&gt; and comment in the particles.js import. This is what we will be making.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2FiMac7%2FDraft%2Fassets%2F76876702%2F6ea0348c-4470-44cb-a6c1-8dfc8872dddb" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2FiMac7%2FDraft%2Fassets%2F76876702%2F6ea0348c-4470-44cb-a6c1-8dfc8872dddb" alt="Screenshot_20240403_155957"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Remember that an image is made up of pixels? Well, for this it's hard to use individual pixels for each particle of the image, that would be a very expensive operation. We'll take the performance hit on this demo and pixelate the image a bit, but it still looks cool :P&lt;/p&gt;

&lt;p&gt;Most of the code will just be defining the 2 classes in use - Particle and Animation. A class is just a way to reuse functionality by only defining one type of something. In this case, every particle of the image has a similar structure.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Animation {
    constructor(width, height, image, context) {
        this.width = width
        this.height = height
        this.particlesArray = []
        this.image = image
        this.pixelSize = 14
        this.context = context
    }
    init() {
        this.image.onload = ()=&amp;gt; {
            //draw the image, split into rectangular pixels,
            //use pixel coordinates + color to give attributes to every particle
            this.context.drawImage(this.image, 0, 0)

            const pixels = this.context.getImageData(0, 0, this.width, this.height)
            const pixelData = pixels.data

            for (let x = 0; x &amp;lt; this.height; x += this.pixelSize) {
                for(let y = 0; y &amp;lt; this.width; y += this.pixelSize) {
                    //get the index of a particle
                    const index = (x + y * this.width) * 4
                    const r = pixelData[index]
                    const g = pixelData[index + 1]
                    const b = pixelData[index + 2]
                    const a = pixelData[index + 3]

                    //only add a particle if pixel is not transparent
                    if (a &amp;gt; 0) {
                        const color = `rgb(${r},${g},${b})`
                        this.particlesArray.push(new Particle(this, x, y, color))
                    }
                }
            }
        }
    }
    draw() {
        this.particlesArray.forEach(particle =&amp;gt; particle.draw(this.context))
    }
    update() {
        this.particlesArray.forEach(particle =&amp;gt; particle.update())
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The class &lt;code&gt;Animation&lt;/code&gt; is an object that each particle (instance of class &lt;code&gt;Particle&lt;/code&gt;) contains.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;init()&lt;/code&gt; function converts an image into an array of large particles.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;draw()&lt;/code&gt; assigns an animation object to each particle.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;update()&lt;/code&gt; calls the update function in each particle. We haven't yet seen what that does.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Particle {
    constructor(animation, x, y, color) {
        this.animation = animation
        this.size = this.animation.pixelSize
        this.color = color
        this.speed = .1

        // initial starting position of the pixel
        this.x = Math.random() * this.animation.width
        this.y = Math.random() * this.animation.height

        // final position of the pixel on the canvas
        this.originX = Math.floor(x)
        this.originY = Math.floor(y)
    }

    draw(context) {
        context.fillStyle = this.color
        context.fillRect(this.x, this.y, this.size, this.size)
    }

    update() {
        this.x += (this.originX - this.x) * this.speed
        this.y += (this.originY - this.y) * this.speed
    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The class &lt;code&gt;Particle&lt;/code&gt; is one of those tiny pieces of an image that you can see floating around. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;draw()&lt;/code&gt; creates a rectangle shape for each particle on the canvas using its x, y starting position and various attributes you can change and have fun with.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;update()&lt;/code&gt; changes each particle's position towards its original position (originX, originY).&lt;/p&gt;

&lt;p&gt;Finally to tie these classes together with some more Javascript.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const canvas = document.querySelector('.myCanvas')
canvas.width = 800
canvas.height = 500

const ctx = canvas.getContext('2d')

// the png format is great for this because it can have transparent pixels.
// This also gives a performance boost in reducing the number 
const imageUrl = 'my-pic-url.png'
const img = new Image()
img.src = imageUrl


const animation = new Animation(canvas.width, canvas.height, img, ctx)
animation.init()
animation.draw()

function animate() {
    // remove all particles from the canvas. 
    // remove comment from next line to delete previous particles.
    // ctx.clearRect(0, 0, canvas.width, canvas.height)

    // draw new particles with updated positions
    animation.draw()
    animation.update()

    //call `animate` about 60 times per second, smooth transition every frame
    requestAnimationFrame(animate)
}
animate()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is a view of the code in full and the result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Animation {
    constructor(width, height, image, context) {
        this.width = width
        this.height = height
        this.particlesArray = []
        this.image = image
        this.pixelSize = 10
        this.context = context
    }
    init() {
        this.image.onload = ()=&amp;gt; {
            //draw the image, split into rectangular pixels,
            //use pixel coordinates + color to give attributes to every particle
            this.context.drawImage(this.image, 0, 0)

            const pixels = this.context.getImageData(0, 0, this.width, this.height)
            const pixelData = pixels.data

            for (let x = 0; x &amp;lt; this.height; x += this.pixelSize) {
                for(let y = 0; y &amp;lt; this.width; y += this.pixelSize) {
                    //get the index of a particle
                    const index = (x + y * this.width) * 4
                    const r = pixelData[index]
                    const g = pixelData[index + 1]
                    const b = pixelData[index + 2]
                    const a = pixelData[index + 3]

                    //only add a particle if pixel is not transparent
                    if (a &amp;gt; 0) {
                        const color = `rgb(${r},${g},${b})`
                        this.particlesArray.push(new Particle(this, x, y, color))
                    }
                }
            }
        }
    }
    draw() {
        this.particlesArray.forEach(particle =&amp;gt; particle.draw(this.context))
    }
    update() {
        this.particlesArray.forEach(particle =&amp;gt; particle.update())
    }
}



class Particle {
    constructor(animation, x, y, color) {
        this.animation = animation
        this.size = this.animation.pixelSize
        this.color = color
        this.speed = .05        

        // initial starting position of the pixel
        this.x = Math.random() * this.animation.width
        this.y = Math.random() * this.animation.height

        // final position of the pixel on the canvas
        this.originX = Math.floor(x)
        this.originY = Math.floor(y)
    }

    draw(context) {
        context.fillStyle = this.color
        context.fillRect(this.x, this.y, this.size, this.size)
    }

    update() {
        this.x += (this.originX - this.x) * this.speed
        this.y += (this.originY - this.y) * this.speed
    }

}





const canvas = document.querySelector('.myCanvas')
canvas.width = 800
canvas.height = 500
const ctx = canvas.getContext('2d')

const imageUrl = 'dubai.jpg'
const img = new Image()
img.src = imageUrl

const animation = new Animation(canvas.width, canvas.height, img, ctx)
animation.init()
animation.draw()

function animate() {
    // comment this line back in to clear out the extra particles
    // ctx.clearRect(0, 0, canvas.width, canvas.height)
    animation.draw()
    animation.update()
    requestAnimationFrame(animate)
}
animate()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2FiMac7%2FDraft%2Fassets%2F76876702%2Fa5626c11-ab99-47b6-b1db-6c7cf9443b8c" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fgithub.com%2FiMac7%2FDraft%2Fassets%2F76876702%2Fa5626c11-ab99-47b6-b1db-6c7cf9443b8c" alt="Screenshot_20240403_160235"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Visit &lt;a href="https://codesandbox.io/p/sandbox/staging-lake-lwpj3h" rel="noopener noreferrer"&gt;the sandbox I made this in&lt;/a&gt; to see it live.&lt;/p&gt;

</description>
      <category>canvas</category>
      <category>pixel</category>
      <category>image</category>
      <category>effects</category>
    </item>
  </channel>
</rss>
