<?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: Conmann</title>
    <description>The latest articles on DEV Community by Conmann (@con).</description>
    <link>https://dev.to/con</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%2F996222%2F9e6894f8-1739-40af-a22f-1b5c9e6af8e4.png</url>
      <title>DEV Community: Conmann</title>
      <link>https://dev.to/con</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/con"/>
    <language>en</language>
    <item>
      <title>Getting pixel color from canvas</title>
      <dc:creator>Conmann</dc:creator>
      <pubDate>Sat, 25 Feb 2023 07:30:13 +0000</pubDate>
      <link>https://dev.to/con/getting-pixel-color-from-canvas-270h</link>
      <guid>https://dev.to/con/getting-pixel-color-from-canvas-270h</guid>
      <description>&lt;p&gt;Ever had a problem getting the color of a pixel from a canvas? Well I spent a week figuring it out so you don't have to.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getColorOnCanvas(x, y, canvasElement){
    let imageData = canvasElement.getContext('2d').getImageData(0,0,canvasElement.width, canvasElement.height).data;
    let f = function(x1,y1,width){return y1 * (width * 4) + x1 * 4;}

    let red = imageData[f(x,y,canvasElement.width)];
    let green = imageData[f(x,y,canvasElement.width)+1];
    let blue = imageData[f(x,y,canvasElement.width)+2];
    let alpha = imageData[f(x,y,canvasElement.width)+3];

    return {
        red: red,
        green: green,
        blue: blue,
        alpha: alpha
    };
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;or, if you're feeling fancy&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getColorOnCanvas(x, y, canvasElement, canvasContext){
    let imageData = canvasContext.getImageData(0,0,canvasElement.width, canvasElement.height).data;
    let f = function(x1,y1,width){return y1 * (width * 4) + x1 * 4;}

    let red = imageData[f(x,y,canvasElement.width)];
    let green = imageData[f(x,y,canvasElement.width)+1];
    let blue = imageData[f(x,y,canvasElement.width)+2];
    let alpha = imageData[f(x,y,canvasElement.width)+3];

    return {
        red: red,
        green: green,
        blue: blue,
        alpha: alpha
    };
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
