0

I'm stuck with a problem. I want to convert green drawn strokes to yellow drawn strokes on an image.

So: I have an image, you draw on the image and you see green lines. Then, in another canvas, I want those green lines to be yellow.

I have this so far:

    </canvas>

    <canvas id="ball2" width="600px" height ="600px">

    </canvas>

    <img id="worldmap" src="worldmap.png" />

    <script type="text/javascript">
    // Set variables
    var a_canvas = document.getElementById("ball");
    var context = a_canvas.getContext("2d");

    var a_canvas2 = document.getElementById("ball2");
    var context2 = a_canvas2.getContext("2d");

    var img=document.getElementById("worldmap");

    var mouse_pos_x = [];
    var mouse_pos_y = [];

    var thickness = 0;

    var arraycount = 0;

    var mouse_down = false;

    var mouse_skip = [];

    function update(){
    }

    document.body.onmousedown = function(){
        mouse_down = true;
        mouse_pos_x.push(event.clientX);
        mouse_pos_y.push(event.clientY);
        arraycount += 1;
    }

    document.body.onmouseup = function() {
        if(mouse_down){
            mouse_down = false;
            mouse_skip.push(arraycount);
        }
    }

    document.body.onmousemove = function() {
        if(mouse_down){
            context.drawImage(img,0,0);
            mouse_pos_x.push(event.clientX);
            mouse_pos_y.push(event.clientY);
            context.lineWidth=2.5;
            context.strokeStyle="#00FF00";
            context.moveTo(mouse_pos_x[arraycount - 1], mouse_pos_y[arraycount - 1]);
            context.lineTo(mouse_pos_x[arraycount], mouse_pos_y[arraycount]);
            context.stroke();
            arraycount += 1;

            var imgdata = context.getImageData(0, 0, a_canvas.width, a_canvas.height);
            var l = imgdata.data.length / 4;

            for (var  i = 0; i < l; i++) {
                var r = imgdata.data[i * 4 + 0];
                var g = imgdata.data[i * 4 + 1];
                var b = imgdata.data[i * 4 + 2];
                if(g < 255){
                imgdata.data[i * 4 + 3] = 0;
            }
        }
        context2.putImageData(imgdata, 0, 0);
        }
    }

    setInterval(update,10);

    </script>

I just can't figure out what's wrong. It draws the image on the left with the green strokes, but nothing on the right. Also, when I remove drawImage() it does work. (It draws the green & yellow line).

EDIT: I made a Fiddle to illustrate my problem: JSFiddle When you remove the <img> tag, everything works. But add it and nothing gets drawn on context2.

By the way, I am aware of the security issue regarding to canvasses. In the Fiddle I use an external domain, but in my own codes I use a local file.

tshepang
  • 12,111
  • 21
  • 91
  • 136
Luud Janssen
  • 362
  • 1
  • 3
  • 12
  • You are nowhere drawing anything to context2. Is this incomplete? Where does (It draws the green & yellow line) happen? On context1 or context2? Better supply a fiddle or plunker ... – mainguy Feb 05 '14 at 14:51
  • You're right... `context.putImageData(imgdata, 0, 0)` has to be `context2.putImageData(imgdata, 0, 0)` but that doesn't solve the problem. I made a Fiddle and edited the post: [JSFiddle](http://jsfiddle.net/ktQtk/) – Luud Janssen Feb 06 '14 at 10:16
  • It took me a while to get your code working in a js testbed. Here is what I have done: Added Jquery, Put everything in a plunker that is here http://plnkr.co/edit/B6eRhRzVH0M6lT2Euqmn?p=preview. Now look at the code there, tell me if this what you want and then we can discuss what could be solved better. Don wonder about the large base64 image. Drawing external images to context is considererd dangerous by plunker. – mainguy Feb 07 '14 at 13:49
  • Btw. To get a result you should open the preview in a new window (Blue x on the top right side). – mainguy Feb 07 '14 at 13:55

0 Answers0