Advertisement
Guest User

Wishbone's Puzzle Solution

a guest
Feb 6th, 2017
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <!doctype html>
  2.  
  3. <html>
  4.     <head>
  5.         <title>The Solution</title>
  6.         <meta charset="UTF-8" />
  7.     </head>
  8.     <body>
  9.         <main>
  10.             <img id="puzzle" src="puzzle.png">
  11.             <canvas id="ourCanvas"></canvas>
  12.         </main>
  13.         <script>
  14.         "use strict";
  15.         window.onload = () => {
  16.             /*
  17.             We begin with a basic HTML file that displays the puzzle image and has
  18.             a canvas which we will use for all of our image manipulations.
  19.             */
  20.  
  21.             // Here we set the context for all subsequesnt manipulations of canvas data
  22.             // as well as the image itself, so that we can draw it on canvas.
  23.             let ctx = document.getElementById("ourCanvas").getContext("2d");
  24.             let puzzleImage = document.getElementById("puzzle")
  25.            
  26.             // Now that we have an image we can use its dimensions to define the height
  27.             // and width of the canvas we are about to use.
  28.             ctx.canvas.width = puzzleImage.width;
  29.             ctx.canvas.height = puzzleImage.height;
  30.  
  31.             // Now we have a ready canvas that we can draw on. Let's start by drawing
  32.             // our puzzle so we can start figuring out the alpha channel business.
  33.             ctx.drawImage(puzzleImage, 0, 0);
  34.  
  35.             // At this point all of the pixel data that interests us is contained
  36.             // within the context, so all we have to do is grab and analyze it.
  37.             let imageData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
  38.  
  39.             // Now we have an array of all pixel data the context contains. Every four
  40.             // elements contain one pixel. [0] is R, [1] is G, [2] is B, and [3] is A.
  41.             // Let's collect our alpha channel data.
  42.             for (let i = 0; i < imageData.data.length; i += 4)
  43.             {
  44.                 if (imageData.data[i+3] < 255) {
  45.                     // This is a pixel with some transparency. Let's save it as black.
  46.                     imageData.data[i] = 0;
  47.                     imageData.data[i+1] = 0;
  48.                     imageData.data[i+2] = 0;
  49.                 } else {
  50.                     // This is a pixel with no transparency. Let's save it as white.
  51.                     imageData.data[i] = 255;
  52.                     imageData.data[i+1] = 255;
  53.                     imageData.data[i+2] = 255;
  54.                 }
  55.  
  56.                 // As we do not want any transparency in the new image we are creating
  57.                 // we will set the alpha channel to non-transparent across all pixels.
  58.                 imageData.data[i+3] = 255;
  59.             }
  60.  
  61.             // At last we can now see what we ended up with. Let's draw this new image!
  62.             ctx.putImageData(imageData, 0, 0);
  63.         };
  64.     </script>
  65.     </body>
  66. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement