HTML/CSS Html5 <canvas> element related apps not working

AI Thread Summary
The discussion centers around creating an image adder in HTML that manipulates pixel RGB values in BMP files using the HTML5 `<canvas>` element. The user encountered issues when trying to implement code from a tutorial on the Mozilla Developer Network, specifically facing cross-origin data errors when using images from external sources. The error, "Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data," indicates that the image being used is not compliant with cross-origin resource sharing (CORS) policies. The user is advised that using images hosted on their own server or ensuring proper CORS settings may resolve the issue. They also mention trying various solutions from Stack Overflow but have not yet succeeded, suggesting that server configuration might be necessary to allow cross-domain data access.
YoungPhysicist
Insights Author
Messages
350
Reaction score
203
Recently I want to make a image adder in html, which is just a app that add the rgb value of each pixel in simple bmp files together.It can be used to grayscales and other things.

I found this website that teches how to do that with the <canvas> element in html5.
https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Pixel_manipulation_with_canvas

The problem is that although there demonstrations work on their webpage, it just don't work when I copied the code from
  • The website using the "view source code" in IE and Edge
  • Their link to CodePen

And NONE OF THE ABOVE works. Not even the CodePen page simulation could work. The only place that their code seems to work is in their page. How is that? Did I miss anything?My code(hippo grayscale and invert):

HTML:
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <link href="https://developer.mozilla.org/static/build/styles/samples.37902ba3b7fe.css" rel="stylesheet" type="text/css" />
       
        <title>Pixel manipulation with canvas - Grayscaling_and_inverting_colors - code sample</title>
    </head>
    <body>
       
            <canvas id="canvas" width="300" height="227"></canvas>
<div>
  <input id="grayscalebtn" value="Grayscale" type="button">
  <input id="invertbtn" value="Invert" type="button">
</div>
       
       
            <script>
                var img = new Image();
img.src = 'https://mdn.mozillademos.org/files/5397/rhino.jpg';
img.onload = function() {
  draw(this);
};
function draw(img) {
  var canvas = document.getElementById('canvas');
  var ctx = canvas.getContext('2d');
  ctx.drawImage(img, 0, 0);
  img.style.display = 'none';
  var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
  var data = imageData.data;
   
  var invert = function() {
    for (var i = 0; i < data.length; i += 4) {
      data[i]     = 255 - data[i];     // red
      data[i + 1] = 255 - data[i + 1]; // green
      data[i + 2] = 255 - data[i + 2]; // blue
    }
    ctx.putImageData(imageData, 0, 0);
  };
  var grayscale = function() {
    for (var i = 0; i < data.length; i += 4) {
      var avg = (data[i] + data[i + 1] + data[i + 2]) / 3;
      data[i]     = avg; // red
      data[i + 1] = avg; // green
      data[i + 2] = avg; // blue
    }
    ctx.putImageData(imageData, 0, 0);
  };
  var invertbtn = document.getElementById('invertbtn');
  invertbtn.addEventListener('click', invert);
  var grayscalebtn = document.getElementById('grayscalebtn');
  grayscalebtn.addEventListener('click', grayscale);
}
            </script>
       
    </body>
</html>
Which is exactly what they posted on codepen.
 
Technology news on Phys.org
Last edited:
  • Like
Likes sysprog
DaveC426913 said:
Both Codepen and JSFiddle are throwing the cross-origin data error:

Uncaught DOMException: Failed to execute 'getImageData' on 'CanvasRenderingContext2D': The canvas has been tainted by cross-origin data.

It's the image that's the problem.

https://stackoverflow.com/questions...-canvas-has-been-tainted-by-cross-origin-data
So if I use other pics, it would be just fine?
 
Read the stackoverflow comments and experiment.
Search for similar questions. There are a few.

I tried a few of their solutions but couldn't get any to work. It might still be a x-domain issue, and it might go away in your dev environment at least.
You may have to add some config to your server to allow x-domain data.
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
1
Views
2K
Replies
5
Views
2K
Replies
8
Views
6K
Replies
13
Views
2K
Replies
3
Views
3K
Replies
8
Views
2K
Replies
2
Views
2K
Replies
12
Views
4K
Replies
2
Views
5K
Back
Top