Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to get correct data from canvas getImageData
    primarykey
    data
    text
    <p>This is in continuation to question <a href="https://stackoverflow.com/questions/16779437/how-to-get-pixel-data-from-canvas-which-contains-the-screenshot-generated-by-a-c/16783347">How to get pixel data from canvas which contains the screenshot generated by a chrome extension?</a> and the source code is available here <a href="https://github.com/whizzzkid/ColorHelper-Chrome/" rel="nofollow noreferrer">https://github.com/whizzzkid/ColorHelper-Chrome/</a></p> <p>The issue I am facing here is, the color I was expecting it to output is not correct and seems to be rather random values. Any help on this will be appreciated.</p> <pre><code>active = false; imgData = canvas = canvasContext = pixelData = ""; chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { if(request.action == 'toggle'){ if(active){ active = false; $('#colorInfoContainer').remove(); console.log(active); }else{ active = true; $('body').prepend("&lt;div id=\"colorInfoContainer\" style=\"color: #000; text-align: center; top:0; width: 200px; position: fixed; left: 60%; margin-left: -100px; height: 15px; margin-top: -5px; -moz-border-radius: 5px; -webkit-border-radius: 5px; background: #fff; padding: 5px 5px 5px; font: bold 14px Arial; border: 3px solid; z-index: 999999999999;\"&gt;&lt;div id=\"colorInfo\" style=\"display: block;\"&gt;Color Info&lt;/div&gt;&lt;/div&gt;"); console.log(active); fill_canvas(); } } }); function fill_canvas(){ if(active){ chrome.extension.sendMessage({action: 'capture'}, function(response) { canvas = document.createElement('canvas'); canvas.width= document.documentElement.clientWidth; canvas.height= document.documentElement.clientHeight; canvasContext = canvas.getContext('2d'); var img = new Image(); img.onload = function(){ canvasContext.drawImage(img, 0, 0); } img.src = response.imageUrl; console.log(img.src); }); } } function getPosition(element) { var xPosition = 0; var yPosition = 0; while(element) { xPosition += (element.offsetLeft - element.scrollLeft + element.clientLeft); yPosition += (element.offsetTop - element.scrollTop + element.clientTop); element = element.offsetParent; } return { x: xPosition, y: yPosition }; } $(document).ready(function(){ setInterval(fill_canvas,1000); $(window).mousemove(function(e) { if(active){ //cache the position of canvas: var canvasPos = getPosition(canvas), dx = canvasPos.x, dy = canvasPos.y; //getting the correct color from pixels var x = e.clientX - dx, y = e.clientY - dy; //force x and y to stay within canvas: x = (x &lt; 0) ? 0 : (x &gt; canvas.width - 1) ? canvas.width - 1 : x; y = (y &lt; 0) ? 0 : (y &gt; canvas.height - 1) ? canvas.height - 1 : y; pixelData = canvasContext.getImageData(x, y, 1, 1).data; //console.log(canvasContext); //pixelData = canvasContext.getImageData(e.clientX,e.clientY, 1, 1).data; //console.log(x+','+y); console.log(pixelData); $('#colorInfoContainer').css('background-color','rgba('+pixelData[0]+', '+pixelData[1]+', '+pixelData[2]+', '+pixelData[3]+')') } }); }); </code></pre> <p>background.js</p> <pre><code>chrome.browserAction.onClicked.addListener(function(tab) { console.log('button clicked'); chrome.tabs.sendMessage(tab.id, { action: 'toggle' }); }); chrome.extension.onMessage.addListener(function(request, sender, sendResponse) { if (request.action == 'capture') { chrome.tabs.captureVisibleTab(null, { 'format':'png' }, function(dataUrl) { sendResponse({ imageUrl: dataUrl }); }); } return true; }); </code></pre> <p>The code mentioned above creates a div which should be of the same color as of the one under the mouse.</p> <p><strong>EDITS: updated according to updates from @Ken - Abdias Software. But still fails. help needed!</strong> Cheers!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload