HTML Canvas 从背景中删除方块
我正在尝试按以下方式在画布上绘制位
function drawOnCanvas(hex){
const ctx = canvas.getContext("2d");
canvas.height = (30/256) * canvas.width;
ctx.fillStyle = 'black';
const portion_width = canvas.width / 256;
const portion_height = canvas.height / 30;
const bytes = hexToBytes(hex);
let x = 0, y = 0;
for(let byte of bytes) {
for(let i = 0 ; i < 8 ; i++) {
if(byte & (1 << i)) {
ctx.fillRect(x, y, portion_width, portion_height);
}
x += portion_width;
if(x >= canvas.width) {
x = 0;
y += portion_height;
}
}
}
}
这是 html 部分
<canvas bind:this={canvas}></canvas>
<style>
canvas {
border: 2px solid red;
width:100%;
}
</style>
这是结果
这是在 python 中解码为图像的相同字节。
我正在尝试删除背景中的方块,但没有成功,任何帮助都会得到帮助。 该应用程序写得很简洁,但我认为它与我的问题没有任何关系。
I am trying to paint bits on canvas, the following way
function drawOnCanvas(hex){
const ctx = canvas.getContext("2d");
canvas.height = (30/256) * canvas.width;
ctx.fillStyle = 'black';
const portion_width = canvas.width / 256;
const portion_height = canvas.height / 30;
const bytes = hexToBytes(hex);
let x = 0, y = 0;
for(let byte of bytes) {
for(let i = 0 ; i < 8 ; i++) {
if(byte & (1 << i)) {
ctx.fillRect(x, y, portion_width, portion_height);
}
x += portion_width;
if(x >= canvas.width) {
x = 0;
y += portion_height;
}
}
}
}
Here is the html secetion
<canvas bind:this={canvas}></canvas>
<style>
canvas {
border: 2px solid red;
width:100%;
}
</style>
Here is the result
This is the same bytes decoded into image in python.
I am trying to remove but with no succsess the squares in the background, any help will be appericiated.
The Applicaiton is written in svelte, But I don't think its have anything to do with my problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与每个像素绘制单独的矩形相比,使用 Canvas get/putImageData 调用,然后可以使用单个 blit 进行绘制。
这些方块可能是由于对更大的图像进行下采样而产生的莫尔图案,但如果没有有关源图像数据的更多信息,很难说出这些伪影的来源。
以下是使用获取/放置图像数据调用的示例:
Rather than drawing individual rectangles per pixel, it would be far more efficient to build the new image using the Canvas get/putImageData calls, then it can be drawn with a single blit.
The squares could be a Moiré pattern caused by down-sampling a much larger image, but without more information on the source image data it's tricky to say where the artefacts are coming from.
Here's an example of using the get/put image data calls: