Commit 36e5daf3 authored by rockyl's avatar rockyl

offX和offY应用上去

parent 5352e9ee
......@@ -2,11 +2,11 @@
* Created by rockyl on 2018/7/12.
*/
import Bounds from "../support/Bounds";
import HashObject from "../core/HashObject";
import {createCanvas} from "./context/RenderContext";
import {Frame} from "../ReType";
import {dirtyFieldDetector} from "../tools/decorators";
import {dirtyFieldDetector, dirtyFieldTrigger} from "../tools/decorators";
import {utils} from "../tools";
/**
* 纹理类
......@@ -15,7 +15,23 @@ export default class Texture extends HashObject {
@dirtyFieldDetector
img: any;
private _bounds: Bounds;
@dirtyFieldTrigger
x: number;
@dirtyFieldTrigger
y: number;
@dirtyFieldTrigger
width: number;
@dirtyFieldTrigger
height: number;
@dirtyFieldTrigger
offX: number = 0;
@dirtyFieldTrigger
offY: number = 0;
@dirtyFieldTrigger
sourceW: number;
@dirtyFieldTrigger
sourceH: number;
private _cacheCanvas;
private _cacheContext;
......@@ -23,9 +39,6 @@ export default class Texture extends HashObject {
super();
this['isDirty'] = true;
this._bounds = new Bounds(0, 0, 0, 0, () => {
this['isDirty'] = true;
});
}
/**
......@@ -33,8 +46,13 @@ export default class Texture extends HashObject {
* @param frame
*/
setFrame(frame: Frame) {
let {x, y, w, h} = frame;
this._bounds.setTo(x, y, w, h);
utils.injectProp(this, frame);
if (!frame.hasOwnProperty('sourceW')) {
frame.sourceW = frame.width;
}
if (!frame.hasOwnProperty('sourceH')) {
frame.sourceH = frame.height;
}
}
/**
......@@ -45,27 +63,6 @@ export default class Texture extends HashObject {
this.img = img;
}
/**
* 获取纹理宽度
*/
get width() {
return this._bounds.width;
}
/**
* 获取纹理高度
*/
get height() {
return this._bounds.height;
}
/**
* 获取边界
*/
get bounds(): Bounds {
return this._bounds;
}
/**
* 产生一个缓存画布
*/
......@@ -75,7 +72,7 @@ export default class Texture extends HashObject {
if (this['isDirty']) {
this['isDirty'] = false;
const {_bounds: {width, height}} = this;
const {width, height} = this;
if (!canvas) {
canvas = this._cacheCanvas = createCanvas();
......@@ -89,8 +86,6 @@ export default class Texture extends HashObject {
canvas.width = width;
canvas.height = height;
this.drawToCanvas(context);
this.drawToCanvas(context);
}
return canvas;
......@@ -107,9 +102,9 @@ export default class Texture extends HashObject {
* @param dh
*/
drawToCanvas(context, dx = 0, dy = 0, sx?, sy?, dw?, dh?) {
const {_bounds: {x, y, width, height}} = this;
const {x, y, width, height, offX, offY} = this;
context.drawImage(this.img, sx || x, sy || y, width, height, dx, dy, dw || width, dh || height);
context.drawImage(this.img, sx || x, sy || y, width, height, dx + offX, dy + offY, dw || width, dh || height);
}
/**
......@@ -117,7 +112,6 @@ export default class Texture extends HashObject {
*/
destroy() {
this.img = null;
this._bounds = null;
this.destroyCacheCanvas();
}
......@@ -138,7 +132,7 @@ export default class Texture extends HashObject {
export function createTexture(img, frame?: Frame): Texture {
const texture = new Texture();
texture.setImg(img);
texture.setFrame(frame || {x: 0, y: 0, w: img.width, h: img.height});
texture.setFrame(frame || {x: 0, y: 0, width: img.width, height: img.height});
return texture;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment