回帖:我用typescript写的web版本电池控件的代码,来向原版致敬。
update(): void {
let context: CanvasRenderingContext2D = this.context;
let width: number = this.canvasWidth;
let height: number = this.canvasHeight;
context.clearRect(0, 0, width, height);
context.save();
//绘制边框
this.drawBorder(context);
//绘制背景
this.drawBg(context);
//绘制头部
this.drawHead(context);
context.restore();
}
protected drawBorder(context: CanvasRenderingContext2D): void {
context.save();
let width: number = this.canvasWidth;
let height: number = this.canvasHeight;
let headWidth: number = width / 10;
let batteryWidth: number = width - headWidth;
//绘制电池边框
let topLeft: Point = new Point(5, 5);
let bottomRight: Point = new Point(batteryWidth, height - 5);
this.batteryRect = new Rect(topLeft, bottomRight);
context.strokeStyle = this.borderColorStart.name;
context.lineWidth = 5;
let painter = new Painter(context);
painter.drawRoundedRect(this.batteryRect.left, this.batteryRect.top,
this.batteryRect.width, this.batteryRect.height,
this.borderRadius, DrawMode.Stroke);
context.restore();
}
protected drawBg(context: CanvasRenderingContext2D): void {
context.save();
let batteryGradient: CanvasGradient = context.createLinearGradient(0, 0, 0, this.canvasHeight);
if (this.currentValue