简体   繁体   中英

Code stops after getImageData

I'm trying to make a colorpicker. My code somehow stops after the getImageData. I couldn't find the solution...

Is there another way to get the value of the pixel? Or the problem is that I'm trying to use a link for the picture?

> function drawImage()   {
>     var image = new Image();
>     image.src = imageSrc;
>     image.onload = function() {
>       context.clearRect(0, 0, canvas.width, canvas.height);
>       context.drawImage(image, 0, 0, image.width, image.height);
> 
>       $('#cPicker').mousemove(function(e) {
>         if (previewOn)                        
>         {
>           var canvasOffset = $(canvas).offset();
>           var x = Math.floor(e.pageX - canvasOffset.left);
>           var y = Math.floor(e.pageY - canvasOffset.top);
> 
>           alert(x + ', ' + y);
>           
>           var imageD = context.getImageData(x, y, 1, 1);
>           var pixel = imageD.data;
>            
>           var pixelColor = 'rgb(' + pixel[0] + ', ' + pixel[1] + ', ' + pixel[2] + ')';
>           $('preview').css('backgroundColor', pixelColor);
> 
>           $('#rValue').val(pixel[0]);
>           $('#gValue').val(pixel[1]);
>           $('#bValue').val(pixel[2]);
>           $('#rgbValue').val(pixel[0]+','+pixel[1]+','+pixel[2]);
> 
>           var dColor = pixel[2] + 256 * pixel[1] + 65536 * pixel[0];
>           $('#hexValue').val('#' + ('0000' + dColor.toString(16)).substr(-6));
>         };
>       });
> 
>       $('#cPicker').click(function(e) {
>         previewOn = !previewOn;  
>       });
>     }; 
  };

Seems like a duplicate of: Saving canvas with CanvasRenderingContext2D filter

I also created a fiddle for testing: https://jsfiddle.net/k6Laknva/1/

var imageSrc = "https://jsfiddle.net/img/logo.png";
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var previewOn = true;

 function drawImage()   {
     var image = new Image();
     image.src = imageSrc;
     image.onload = function() {
       context.clearRect(0, 0, canvas.width, canvas.height);
       context.drawImage(image, 0, 0, image.width, image.height);

       $('#cPicker').mousemove(function(e) {
         if (previewOn) {
           var canvasOffset = $(canvas).offset();
           var x = Math.floor(e.pageX - canvasOffset.left);
           var y = Math.floor(e.pageY - canvasOffset.top);

           console.log(x + ', ' + y);

           var imageD = context.getImageData(x, y, 1, 1);
           var pixel = imageD.data;

           var pixelColor = 'rgb(' + pixel[0] + ', ' + pixel[1] + ', ' + pixel[2] + ')';
           $('preview').css('backgroundColor', pixelColor);

           $('#rValue').val(pixel[0]);
           $('#gValue').val(pixel[1]);
           $('#bValue').val(pixel[2]);
           $('#rgbValue').val(pixel[0]+','+pixel[1]+','+pixel[2]);

           var dColor = pixel[2] + 256 * pixel[1] + 65536 * pixel[0];
           $('#hexValue').val('#' + ('0000' + dColor.toString(16)).substr(-6));
         };
       });

       $('#cPicker').click(function(e) {
         previewOn = !previewOn;  
       });
     }; 
  };

  drawImage();

This runs fine in Chrome 56, but fails in Firefox 52 :/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM