I am trying to create a canvas on a page that updates itself whenever a user draws something on it. However, it looks like the following code fragment (from index.html) is causing an error
socket.on("drawn_complete",function(data){
ctx.putImageData(data.image,0,0);
console.log("Everthing worked technically");
});
Here is the error that appears on my console:
Uncaught TypeError: Failed to execute 'putImageData' on 'CanvasRenderingContext2D': No function was found that matched the signature provided
my data.image is what is returned from the following code:
ctx.getImageData(0,0,200,100); // ctx is canvas.getContext("2d")
Here is my canvas in my html:
<canvas id="myCanvas" width="200" height="100"
style="border:1px solid #000000;">
</canvas>
Here is the complete JS node:
var http = require("http")
var server = http.createServer(handler).listen(1337);
console.log("Server created at 127.0.0.1:1337");
var io = require("socket.io").listen(server);
function handler(req,res)
{
console.log("Client Connected");
res.writeHead(200,{"Content-type": "text/plain"});
res.end("Hello World");
}
io.sockets.on("connection",function(socket){
socket.on("drawing",function(data){
var image = data["image"];
io.sockets.emit("drawn_complete",image);
});
});
Drawing is done when the user holds and moves the mouse inside my canvas:
Here is the code:
c.onmousedown = function(evt){
moving = true
};
c.onmousemove = function(evt){
if(moving == true)
{
console.log("holding and moving");
var x = evt.clientX - rect.left;
var y = evt.clientY - rect.top;
console.log("X: " + x + " Y: " + y);
ctx.fillRect(x,y,1,1);
socket.emit("drawing",{"image": ctx.getImageData(0,0,200,100)});
}
};
c.onmouseup = function(evt){
moving = false;
};
UPDATE
Here is the enitre script:
<script>
window.onload = function(){
var socket = io.connect("http://localhost:1337");
socket.on("drawn_complete",function(data){
ctx.putImageData(data.image,0,0);
console.log("Everthing worked technically");
});
var c = document.getElementById("myCanvas");
var moving = false;
console.log(c);
var ctx = c.getContext("2d");
var rect = c.getBoundingClientRect();
c.onmousedown = function(evt){
moving = true
};
c.onmousemove = function(evt){
if(moving == true)
{
console.log("holding and moving");
var x = evt.clientX - rect.left;
var y = evt.clientY - rect.top;
console.log("X: " + x + " Y: " + y);
ctx.fillRect(x,y,1,1);
socket.emit("drawing",{"image": ctx.getImageData(0,0,200,100)});
}
};
c.onmouseup = function(evt){
moving = false;
};
};
</script>
UPDATE
As Palahniuk suggested, I tried serializing the image before fixing it:
var stringfy = JSON.stringify(ctx.getImageData(0,0,200,100));
socket.emit("drawing",{"image": stringfy});
It looks like the console is throwing the same error anyway
(2)
@Palanik, . , , , - :
:
var stringfy = ctx.getImageData(0,0,200,100);
console.log(stringfy)
var sent = JSON.stringify(stringfy);
socket.emit("drawing",{"image": sent});
socket.on("drawn_complete",function(data){
imageData = JSON.parse(data.image);
console.log(imageData);
ctx.putImageData(imageData,0,0);
console.log("Everthing worked technically");
});
, console.log of imageData Oject , ImageData. , . :
{"height":100,"width":200,"data":{"0":0,"1":0,"2":0,"3":0,"4":0,"5 etc... }
, , . java, ,
ImageData object = (ImageData) imageData