用纯Javascript创建画布/位图(无html)

Fre*_*ddy 1 javascript canvas bitmap node.js socket.io

我正在使用socket.io为Javascript开发iPhone游戏服务器。服务器的目的是使用玩家路径绘制屏幕外的位图,以检查该路径是否已绘制。简而言之,所有图形只会显示在客户端屏幕上。这是我发现的用于创建画布,然后在其中查找像素颜色的代码。但是我没有html代码,因为它仅使用Javascript制作。那么,这些代码是否可以在仅Javascript程序中工作?如果没有,我该怎么做但结果相同?

编辑:我正在使用socket.io与node.js

var canvas = document.createElement("canvas");
var context = canvas.getContext("2d");
// Make sure to set the size, otherwise its zero
canvas.width = 100;
canvas.height = 100;

// Draw to the offscreen canvas
context.fillStyle = "#0000ff";
context.fillRect(0,0,50,50);
context.fillStyle = "#ff9900";
context.arc(50,50,25,50,0,Math.PI*2);
context.fill();
//  document.body.appendChild(canvas)
// To preview the canvas
var imgData = context.getImageData(0, 0, canvas.height, canvas.width);
var offset =  90*canvas.width+50*4

  console.log(imgData.data[offset]);
  console.log(imgData.data[offset+1]);
  console.log(imgData.data[offset+2]);
  console.log(imgData.data[offset+3]); 
Run Code Online (Sandbox Code Playgroud)

mar*_*rkE 5

Node.JS + Node-Canvas将接受纯JavaScript输入并输出图像:

https://github.com/Automattic/node-canvas