forked from SimonShiki/raphael
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathctx-manager.js
87 lines (76 loc) · 2.73 KB
/
ctx-manager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import StageLayering from './stage-layering';
class CtxManager {
constructor (runtime) {
this.runtime = runtime;
this._drawable = [];
this._penSkinId = -1;
this.initialize();
}
shouldCreatePenLayer () {
if (this._penSkinId < 0) return true;
if (!this.runtime.renderer._allSkins[this._penSkinId]) return true;
return false;
}
get penSkinId () {
if (this.shouldCreatePenLayer() && this.runtime.renderer) {
this._penSkinId = this.runtime.renderer.createPenSkin();
this._penDrawableId = this.runtime.renderer.createDrawable(StageLayering.PEN_LAYER);
this.runtime.renderer.updateDrawableSkinId(this._penDrawableId, this._penSkinId);
}
return this._penSkinId;
}
/**
* 创建画布
*/
createCanvas () {
const [w, h] = this.runtime.renderer._allSkins[this.penSkinId].size;
const canvas = document.createElement("canvas"); // 创建canvas元素
canvas.width = w;
canvas.height = h;
return canvas;
}
/**
* 初始化
*/
initialize () {
// 创建初始画布
this._drawable = [];
const canvas = this.createCanvas();
if (!canvas) throw new Error("CanvasEngine: Can't create canvas");
this._drawable[0] = canvas;
const stampCanvas = this.createCanvas();
this._stampStuff = {
skinId: this.runtime.renderer.createBitmapSkin(stampCanvas, 1),
drawableId: this.runtime.renderer.createDrawable(StageLayering.PEN_LAYER)
}
this.runtime.renderer.updateDrawableSkinId(this._stampStuff.drawableId, this._stampStuff.skinId);
this.runtime.renderer.updateDrawableVisible(this._stampStuff.drawableId, false);
}
/**
* 获取指定id画布
* 按照规定只应通过此函数获取画布示例
* @returns {Canvas} 画布
* @throws {Error} 如果id不存在则抛出异常
*/
getContext (id) {
if (!this._drawable[id]) {
const canvas = this.createCanvas();
if (!canvas) throw new Error("CanvasEngine: Can't create canvas");
this._drawable[id] = canvas;
}
return this._drawable[id].getContext('2d');
}
/**
* 将指定画布打印至舞台
*/
stampOnStage (id) {
if (!this.penSkinId) return;
const ctx = this.getContext(id);
if (!ctx) return;
const imageData = ctx.getImageData(0, 0, 480, 360);
this.runtime.renderer._allSkins[this.penSkinId]._setTexture(imageData);
this.runtime.renderer.penStamp(this.penSkinId, this._stampStuff.drawableId);
this.runtime.requestRedraw();
}
}
export default CtxManager;