mirror of
https://github.com/motajs/template.git
synced 2026-11-25 23:44:37 +08:00
refactor: 删除 canvas2d 对 eventemitter3 的依赖
This commit is contained in:
1 parent
9e6ec39859
commit
bbb1eec0fa
2 files changed
+8
-15
No files matched your search
@@ -1,11 +1,4 @@
|
||||
import { EventEmitter } from 'eventemitter3';
|
||||
|
||||
interface OffscreenCanvasEvent {
|
||||
/** 当被动触发resize时(例如窗口大小变化)时触发,使用size函数并不会触发 */
|
||||
resize: [];
|
||||
}
|
||||
|
||||
export class MotaOffscreenCanvas2D extends EventEmitter<OffscreenCanvasEvent> {
|
||||
export class MotaOffscreenCanvas2D {
|
||||
canvas: HTMLCanvasElement;
|
||||
ctx: CanvasRenderingContext2D;
|
||||
|
||||
@@ -17,7 +10,7 @@ export class MotaOffscreenCanvas2D extends EventEmitter<OffscreenCanvasEvent> {
|
||||
/** 是否启用抗锯齿 */
|
||||
antiAliasing: boolean = true;
|
||||
|
||||
scale: number = 1;
|
||||
scale: number = 0;
|
||||
|
||||
/** 更新标识符,如果发生变化则说明画布被动清空 */
|
||||
symbol: number = 0;
|
||||
@@ -30,11 +23,11 @@ export class MotaOffscreenCanvas2D extends EventEmitter<OffscreenCanvasEvent> {
|
||||
* @param canvas 指定画布,不指定时会自动创建一个新画布
|
||||
*/
|
||||
constructor(alpha: boolean = true, canvas?: HTMLCanvasElement) {
|
||||
super();
|
||||
this.canvas = canvas ?? document.createElement('canvas');
|
||||
this.ctx = this.canvas.getContext('2d', { alpha })!;
|
||||
this.width = this.canvas.width / devicePixelRatio;
|
||||
this.height = this.canvas.height / devicePixelRatio;
|
||||
this.setScale(1);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -42,10 +35,10 @@ export class MotaOffscreenCanvas2D extends EventEmitter<OffscreenCanvasEvent> {
|
||||
* @param scale 缩放比
|
||||
*/
|
||||
setScale(scale: number) {
|
||||
// if (scale === this.scale) {
|
||||
// this.clear();
|
||||
// return;
|
||||
// }
|
||||
if (scale === this.scale) {
|
||||
this.clear();
|
||||
return;
|
||||
}
|
||||
this.scale = scale;
|
||||
let ratio = this.highResolution ? devicePixelRatio : 1;
|
||||
ratio *= this.scale;
|
||||
|
||||
@@ -245,7 +245,7 @@ export interface IRenderItem<
|
||||
readonly composite: GlobalCompositeOperation;
|
||||
/** 元素不透明度 */
|
||||
readonly alpha: number;
|
||||
/** 元素的缩放比例,与渲染器的缩放比例及 `devicePixelRatio` 等有关 */
|
||||
/** 元素的缩放比例,与渲染器的缩放比例等有关 */
|
||||
readonly scale: number;
|
||||
|
||||
/** 光标样式 */
|
||||
|
||||
Reference in new issue
Block a user