优化取模组件,取模效果更好

This commit is contained in:
liub
2026-04-03 17:24:55 +08:00
parent 1264ec70a2
commit 1e51023c00
9 changed files with 132 additions and 89 deletions

View File

@ -65,7 +65,7 @@
* 清除Canvas内容
*/
clearCanvas() {
this.ctx.setFillStyle(this.bgColor);
this.ctx.setFillStyle('#FFFFFF');//this.bgColor
this.ctx.fillRect(0, 0, this.currentCanvasWidth, this.currentCanvasHeight);
},
@ -82,9 +82,9 @@
this.currentCanvasWidth = 16;
this.currentCanvasHeight = 16;
this.clearCanvas();
this.ctx.setFillStyle(this.color);
this.ctx.setFillStyle('#000000');//this.color
this.ctx.setFontSize(this.fontSize);
this.ctx.font = `${this.fontSize}px "PingFangBold", "PingFang SC", Arial, sans-serif`;
this.ctx.font = `${this.fontSize}px "PingFang SC","PingFangBold", Arial, sans-serif`;
this.ctx.setTextBaseline('middle');
this.ctx.fillText('测', 0, 8);
@ -158,12 +158,20 @@
// pixels.push(R < 128 ? 1 : 0);
const R = imageData[i];
const G = imageData[i + 1];
const B = imageData[i + 2];
const gray = 0.299 * R + 0.587 * G + 0.114 * B; // 灰度转换
pixels.push(gray < 128 ? 1 : 0);
// const R = imageData[i];
// const G = imageData[i + 1];
// const B = imageData[i + 2];
// const gray = 0.299 * R + 0.587 * G + 0.114 * B; // 灰度转换
// pixels.push(gray < 128 ? 1 : 0);
let r = imageData[i];
let g = imageData[i+1];
let b = imageData[i+2];
let gray = (r + g + b) / 3;
let bit = gray < 255 ? 1 : 0;
pixels.push(bit);
}
const lowBytes = new Array(charWidth).fill(0);
@ -197,11 +205,11 @@
this.clearCanvas();
// 3. 设置文字样式
ctx.setFillStyle(this.color);
ctx.setFillStyle('#000000');//this.color
ctx.setTextBaseline('middle');
// ctx.setTextAlign('center')
ctx.setFontSize(this.fontSize);
ctx.font = `${this.fontSize}px "PingFangBold", "PingFang SC", Arial, sans-serif`;
ctx.font = `${this.fontSize}px "PingFang SC","PingFangBold", Arial, sans-serif`;
// 4. 绘制当前行文本
debugger;