Commit 36e5daf3 authored by rockyl's avatar rockyl

offX和offY应用上去

parent 5352e9ee
...@@ -2,11 +2,11 @@ ...@@ -2,11 +2,11 @@
* Created by rockyl on 2018/7/12. * Created by rockyl on 2018/7/12.
*/ */
import Bounds from "../support/Bounds";
import HashObject from "../core/HashObject"; import HashObject from "../core/HashObject";
import {createCanvas} from "./context/RenderContext"; import {createCanvas} from "./context/RenderContext";
import {Frame} from "../ReType"; 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 { ...@@ -15,7 +15,23 @@ export default class Texture extends HashObject {
@dirtyFieldDetector @dirtyFieldDetector
img: any; 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 _cacheCanvas;
private _cacheContext; private _cacheContext;
...@@ -23,9 +39,6 @@ export default class Texture extends HashObject { ...@@ -23,9 +39,6 @@ export default class Texture extends HashObject {
super(); super();
this['isDirty'] = true; this['isDirty'] = true;
this._bounds = new Bounds(0, 0, 0, 0, () => {
this['isDirty'] = true;
});
} }
/** /**
...@@ -33,8 +46,13 @@ export default class Texture extends HashObject { ...@@ -33,8 +46,13 @@ export default class Texture extends HashObject {
* @param frame * @param frame
*/ */
setFrame(frame: Frame) { setFrame(frame: Frame) {
let {x, y, w, h} = frame; utils.injectProp(this, frame);
this._bounds.setTo(x, y, w, h); 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 { ...@@ -45,27 +63,6 @@ export default class Texture extends HashObject {
this.img = img; 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 { ...@@ -75,7 +72,7 @@ export default class Texture extends HashObject {
if (this['isDirty']) { if (this['isDirty']) {
this['isDirty'] = false; this['isDirty'] = false;
const {_bounds: {width, height}} = this; const {width, height} = this;
if (!canvas) { if (!canvas) {
canvas = this._cacheCanvas = createCanvas(); canvas = this._cacheCanvas = createCanvas();
...@@ -89,8 +86,6 @@ export default class Texture extends HashObject { ...@@ -89,8 +86,6 @@ export default class Texture extends HashObject {
canvas.width = width; canvas.width = width;
canvas.height = height; canvas.height = height;
this.drawToCanvas(context); this.drawToCanvas(context);
this.drawToCanvas(context);
} }
return canvas; return canvas;
...@@ -107,9 +102,9 @@ export default class Texture extends HashObject { ...@@ -107,9 +102,9 @@ export default class Texture extends HashObject {
* @param dh * @param dh
*/ */
drawToCanvas(context, dx = 0, dy = 0, sx?, sy?, dw?, 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 { ...@@ -117,7 +112,6 @@ export default class Texture extends HashObject {
*/ */
destroy() { destroy() {
this.img = null; this.img = null;
this._bounds = null;
this.destroyCacheCanvas(); this.destroyCacheCanvas();
} }
...@@ -138,7 +132,7 @@ export default class Texture extends HashObject { ...@@ -138,7 +132,7 @@ export default class Texture extends HashObject {
export function createTexture(img, frame?: Frame): Texture { export function createTexture(img, frame?: Frame): Texture {
const texture = new Texture(); const texture = new Texture();
texture.setImg(img); 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; 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