Tuesday 6 June 2017

NodeJS/HTML5 Draw image not working?

I am making a multiplayer game using NodeJS. Currently, my client file creates the visuals for the game using context and HTML5 canvas. The game works fine when i right click and run it on google chrome. However, when i connect the server and try to run it from local host, its says the image can not be found.

var ctx = canvas.getContext("2d");
var dragon = new Image();
dragon.src = "dragon.png";
ctx.drawImage(dragon, 20, 20, 60, 60);

This is the code for drawing the image. On the server side I simply do this.

var express = require("express");
var app = express();
var server = require("http").Server(app);
var io = require("socket.io")(server, {});

And on connection:

io.sockets.on('connection', function(socket) {
console.log("joined");
});

The console gives me the error 404 not found for my image, but the image is in the folder and it works when I run it without the server. Does someone know what is going on?



via Albert Jin

No comments:

Post a Comment