Html5 <canvas> element related apps not working

Click For Summary

Discussion Overview

The discussion revolves around issues encountered when using the HTML5 element for image manipulation, specifically regarding the inability to execute pixel manipulation code due to cross-origin data errors. Participants explore the technical challenges and potential solutions related to this problem.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their attempt to create an image adder application using the element, but finds that the code works only on the original tutorial page and not when copied to their own environment.
  • Another participant identifies the cross-origin data error as the cause of the issue, explaining that the canvas is tainted by cross-origin data when trying to manipulate images from different origins.
  • Some participants suggest that using different images might resolve the issue, implying that the problem is related to the specific image being used.
  • One participant recommends reading comments on Stack Overflow and experimenting with various solutions, noting that the issue may be related to cross-domain configurations that could be adjusted on the server side.

Areas of Agreement / Disagreement

Participants generally agree that the cross-origin data error is a significant issue affecting the functionality of the code, but there is no consensus on the best approach to resolve it or whether using different images will definitively fix the problem.

Contextual Notes

The discussion highlights potential limitations related to cross-origin resource sharing (CORS) and the need for server configuration to allow cross-domain data access, which remains unresolved.

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   Reactions: sysprog
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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
6K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K