49 lines
900 B
JavaScript
49 lines
900 B
JavaScript
const PIXI = 'undefined' !== typeof window ? require('pixi.js') : undefined;
|
|
|
|
import {compose} from '@avocado/core';
|
|
import {Vector} from '@avocado/math';
|
|
|
|
import {Image} from './image';
|
|
|
|
const decorate = compose(
|
|
Vector.Mixin('size', 'width', 'height', {
|
|
default: [0, 0],
|
|
}),
|
|
);
|
|
|
|
export class Canvas extends decorate(class {}) {
|
|
|
|
constructor(size = [0, 0]) {
|
|
super();
|
|
this.renderTexture = PIXI.RenderTexture.create(size[0], size[1]);
|
|
}
|
|
|
|
destroy() {
|
|
this.renderTexture.destroy();
|
|
}
|
|
|
|
get internal() {
|
|
return this.renderTexture;
|
|
}
|
|
|
|
render(renderable, renderer) {
|
|
renderer.render(renderable, this);
|
|
}
|
|
|
|
get size() {
|
|
return super.size;
|
|
}
|
|
|
|
set size(size) {
|
|
this.renderTexture.resize(size[0], size[1]);
|
|
super.size = size;
|
|
}
|
|
|
|
toImage() {
|
|
const image = new Image();
|
|
image.texture = this.renderTexture.clone();
|
|
return image;
|
|
}
|
|
|
|
}
|