use of playn.core.CanvasImage in project playn by threerings.
the class TextTest method makeTextImage.
protected Image makeTextImage() {
TextFormat format = new TextFormat(graphics().createFont(font.value(), style.value(), 24), true);
float wrapWidth = wrap.value() == 0 ? Float.MAX_VALUE : graphics().width() * wrap.value() / 100;
TextBlock block = new TextBlock(graphics().layoutText(sample, format, new TextWrap(wrapWidth)));
float awidth = adjustWidth(block.bounds.width()), aheight = adjustHeight(block.bounds.height());
float pad = TextBlock.pad();
CanvasImage image = graphics().createImage(awidth + 2 * pad, aheight + 2 * pad);
image.canvas().translate(pad, pad);
image.canvas().setStrokeColor(0xFFFFCCCC).strokeRect(0, 0, awidth, aheight);
render(image.canvas(), block, align.value(), lineBounds.value());
return image;
}
use of playn.core.CanvasImage in project playn by threerings.
the class AlphaLayerTest method init.
@Override
public void init() {
final GroupLayer rootLayer = graphics().rootLayer();
final float fullWidth = 6 * width, fullHeight = 3 * height;
// add a half white, half blue background
SurfaceImage bg = graphics().createSurface(fullWidth, fullHeight);
bg.surface().setFillColor(Color.rgb(255, 255, 255));
bg.surface().fillRect(0, 0, fullWidth, fullHeight);
bg.surface().setFillColor(Color.rgb(0, 0, 255));
bg.surface().fillRect(0, 2 * height, fullWidth, height);
rootLayer.add(graphics().createImageLayer(bg));
addDescrip("all layers contained in group layer with a=0.5\n" + "thus, fully composited a=0.25", offset, fullHeight + 5, fullWidth);
// add a 50% transparent group layer
final GroupLayer groupLayer = graphics().createGroupLayer();
groupLayer.setAlpha(0.5f);
rootLayer.add(groupLayer);
assets().getImage("images/alphalayertest.png").addCallback(new Callback<Image>() {
@Override
public void onSuccess(Image image) {
// add the layers over the white background
float x = offset;
groupLayer.addAt(graphics().createImageLayer(image).setAlpha(0.5f), x, offset);
addDescrip("image\nimg layer a=0.5", x, offset + height, width);
x += width;
SurfaceImage surf1 = graphics().createSurface(image.width(), image.height());
surf1.surface().setAlpha(0.5f).drawImage(image, 0, 0);
groupLayer.addAt(graphics().createImageLayer(surf1), x, offset);
addDescrip("surface a=0.5\nimg layer a=1", x, offset + height, width);
x += width;
SurfaceImage surf2 = graphics().createSurface(image.width(), image.height());
surf2.surface().drawImage(image, 0, 0);
groupLayer.addAt(graphics().createImageLayer(surf2).setAlpha(0.5f), x, offset);
addDescrip("surface a=1\nimg layer a=0.5", x, offset + height, width);
x += width;
CanvasImage canvas1 = graphics().createImage(image.width(), image.height());
canvas1.canvas().drawImage(image, 0, 0);
groupLayer.addAt(graphics().createImageLayer(canvas1).setAlpha(0.5f), x, offset);
addDescrip("canvas a=1\nimg layer a=0.5", x, offset + height, width);
x += width;
CanvasImage canvas2 = graphics().createImage(image.width(), image.height());
canvas2.canvas().setAlpha(0.5f).drawImage(image, 0, 0);
groupLayer.addAt(graphics().createImageLayer(canvas2), x, offset);
addDescrip("canvas a=0.5\nimg layer a=1", x, offset + height, width);
x += width;
// add the same layers over the blue background
x = offset;
groupLayer.addAt(graphics().createImageLayer(image).setAlpha(0.5f), x, offset + 2 * height);
x += width;
SurfaceImage surf1b = graphics().createSurface(image.width(), image.height());
surf1b.surface().setAlpha(0.5f).drawImage(image, 0, 0);
groupLayer.addAt(graphics().createImageLayer(surf1b), x, offset + 2 * height);
x += width;
SurfaceImage surf2b = graphics().createSurface(image.width(), image.height());
surf2b.surface().drawImage(image, 0, 0);
groupLayer.addAt(graphics().createImageLayer(surf2b).setAlpha(0.5f), x, offset + 2 * height);
x += width;
CanvasImage canvas1b = graphics().createImage(image.width(), image.height());
canvas1b.canvas().drawImage(image, 0, 0);
groupLayer.addAt(graphics().createImageLayer(canvas1b).setAlpha(0.5f), x, offset + 2 * height);
x += width;
CanvasImage canvas2b = graphics().createImage(image.width(), image.height());
canvas2b.canvas().setAlpha(0.5f).drawImage(image, 0, 0);
groupLayer.addAt(graphics().createImageLayer(canvas2b), x, offset + 2 * height);
// add some copies of the image at 1, 0.5, 0.25 and 0.125 alpha
x = offset + width;
for (float alpha : new float[] { 1, 1 / 2f, 1 / 4f, 1 / 8f }) {
float y = fullHeight + 50;
rootLayer.addAt(graphics().createImageLayer(image).setAlpha(alpha), x, y);
addDescrip("image a=" + alpha, x, y + height / 2, width / 2);
x += width;
}
}
@Override
public void onFailure(Throwable err) {
log().error("Error loading image", err);
}
});
// add ground truth of 25% opaque image
assets().getImage("images/alphalayertest_expected.png").addCallback(new Callback<Image>() {
@Override
public void onSuccess(Image image) {
rootLayer.addAt(graphics().createImageLayer(image), 5 * width, 0);
addDescrip("ground truth", 5 * width, offset + height, width);
}
@Override
public void onFailure(Throwable err) {
log().error("Error loading image", err);
}
});
}
use of playn.core.CanvasImage in project playn by threerings.
the class CanvasStressTest method init.
@Override
public void init() {
CanvasImage canvasImage = graphics().createImage(graphics().width(), graphics().height());
canvas = canvasImage.canvas();
graphics().rootLayer().add(graphics().createImageLayer(canvasImage));
}
use of playn.core.CanvasImage in project playn by threerings.
the class CanvasTest method createCanvasImage.
private CanvasImage createCanvasImage(int width, int height, final Drawer drawer) {
final CanvasImage image = graphics().createImage(width, height);
drawer.draw(image.canvas());
return image;
}
use of playn.core.CanvasImage in project playn by threerings.
the class CanvasTest method addTestCanvas.
private void addTestCanvas(String descrip, int width, int height, String imagePath, final ImageDrawer drawer) {
final CanvasImage target = graphics().createImage(width, height);
assets().getImage(imagePath).addCallback(new Callback<Image>() {
public void onSuccess(Image image) {
drawer.draw(target.canvas(), image);
}
public void onFailure(Throwable err) {
System.err.println("Oops! " + err);
}
});
addTestLayer(descrip, width, height, graphics().createImageLayer(target));
}
Aggregations