PlayN中的像素级图形

时间:2012-05-06 11:44:41

标签: canvas playn

我想在主要目标是HTML5的PlayN游戏上进行像素级图形处理。但是,PlayN的Canvas对象无法访问CanvasPixelArray类或putImageDatagetImageData函数。我担心每个像素使用drawPoint会很慢;有更好的方法吗?

1 个答案:

答案 0 :(得分:1)

目前PlayN还没有像素操作API。

如果您只想定位HTML5,可以使用GWT方法创建和操作ImageData对象,然后通过我最近添加的用于转换图像的机制将它们注入PlayN。

// use this canvas and context to create as many image data objects as you want
CanvasElement canvas = Document.get().createElement("canvas").<CanvasElement>cast();
Context2d ctx = canvas.getContext2d();
final ImageData data = ctx.createImageData(width, height);
// push those pixels

// use this PlayN image as a factory for creating PlayN images from your ImageData objects
CanvasImage image = PlayN.graphics().createImage(1, 1); // dummy image
Image pixelImage = image.transform(new HtmlBitmapTransformer() {
  public ImageElement transform(ImageElement elem) {
    // we ignore the element passed in and just create a new CanvasElement
    // and draw our bitmap data on it
    CanvasElement canvas = Document.get().createElement("canvas").<CanvasElement>cast();
    canvas.setWidth(width);
    canvas.setHeight(height);
    canvas.getContext2d().putImageData(data, 0, 0);
    return canvas;
  }
});

这有点像黑客。在某些时候,我将添加一种机制来获取和操作图像的像素数据,这种机制的速度与可以支持它的后端一样快。