Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 31x 31x 31x 31x 31x 31x 12x 12x 12x 12x 12x 31x 31x 3x 3x 31x 31x 3x 3x 31x 31x 1x 1x 31x 31x 31x 31x 31x 31x 31x 31x 3x 3x 3x 3x 31x 31x 31x 31x 31x 31x 1x 1x 1x 1x 31x 31x 31x 31x 31x 31x 31x 31x 1x 1x 1x 1x 1x 1x 1x 1x 1x 31x 31x 31x 31x 31x 31x 1x 1x 1x 31x 31x 31x 31x 31x 31x 1x 1x 31x 31x 31x 31x 31x 31x 31x 31x 1x 1x 31x 31x 31x 31x 31x 31x 31x 31x 1x 1x 31x 31x 31x 31x 31x 31x 31x 31x 1x 1x 31x 31x 31x 31x 31x 1x 1x 31x | import { BaseModel } from './base-model';
import { MessageType } from '../core/message-type';
import { Servicer } from '../servicer/servicer';
import { loadImage, loadImageData } from '../utils/load-image';
import { FillType } from '../enums/fill-type';
const commands = MessageType.source;
/**
* 数据源
*/
export class Source extends BaseModel {
public fillType: FillType = FillType.NONE;
private _width: number = 0;
private _height: number = 0;
constructor(
sourceId: string,
servicer: Servicer
) {
super(sourceId, servicer);
}
get width() {
return this._width;
}
get height() {
return this._height;
}
async destory(): Promise<void> {
await this.servicer.do(commands.destory, [this.id]);
}
/**
* 将像素数据设置到数据源上
* @param pixels - 像素数据
* @param width - 宽度
* @param height - 高度
*/
async setPixels(pixels: ArrayLike<number>, width: number, height: number): Promise<void> {
this._width = width;
this._height = height;
await this.servicer.do(commands.setPixels, [this.id, pixels, width, height]);
}
/**
* 将远端服务上的图片加载并设置到数据源上
* @param url - 图片地址
*/
async setPixelsFromUrl(url: string) {
const image = await loadImage(url);
const imageData = loadImageData(image);
await this.setPixels(imageData.data, imageData.width, imageData.height);
}
/**
* 将纯色画布设置为数据源
* @param color - 画布颜色
* @param width - 宽度
* @param height - 高度
*/
async setPixelsFromColor(color: string, width: number, height: number) {
const canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
const context = canvas.getContext('2d')!;
context.fillStyle = color;
context.fillRect(0, 0, width, height);
const imageData = context.getImageData(0, 0, width, height);
await this.setPixels(imageData.data, width, height);
}
/**
* 设置填充方式
* @param fillType - 填充方式
*/
async setFillType(fillType: FillType) {
this.fillType = fillType;
await this.servicer.do(commands.setFillType, [this.id, fillType]);
}
/**
* 设置基于画布宽高与目标宽高比例的裁剪
* @param ratio - 宽高比例
*/
async setClipRatio(ratio: number) {
await this.servicer.do(commands.setClipRatio, [this.id, ratio]);
}
/**
* 平移数据源
* @param x - x 轴平移量
* @param y - y 轴平移量
* @param z - z 轴平移量
*/
async translate(x: number, y: number, z: number) {
await this.servicer.do(commands.translate, [this.id, x, y, z]);
}
/**
* 缩放数据源
* @param x - x 轴缩放比
* @param y - y 轴缩放比
* @param z - z 轴缩放比
*/
async scale(x: number, y: number, z: number) {
await this.servicer.do(commands.scale, [this.id, x, y, z]);
}
/**
* 旋转数据源
* @param x - x 轴旋转角度
* @param y - y 轴旋转角度
* @param z - z 轴旋转角度
*/
async rotate(x: number, y: number, z: number) {
await this.servicer.do(commands.rotate, [this.id, x, y, z]);
}
/**
* 数据变更时候需要调用该接口通知基类
*/
async notifyRefresh() {
await this.servicer.do(commands.notifyRefresh, [this.id]);
}
} |