使用我的像素艺术程序时的怪异视觉错误
正如标题所述,我的像素艺术程序不断发生一个奇怪的视觉错误。当我使用pushmatrix()
scale> scale> scale()和popmatrix()
popmatrix()时。我无法真正描述这个错误,所以我得到了它的屏幕截图(该错误是像素之间的奇怪的白线)。
代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Pixel Art Drawer & Importer</title>
</head>
<body>
<canvas id="mycanvas"></canvas>
<input type="text" id="color1" value="100" maxlength="3">
<input type="text" id="color2" value="100" maxlength="3">
<input type="text" id="color3" value="100" maxlength="3">
<p>You can import the pixel art textures into a game or program with this <a href="https://www.khanacademy.org/computer-programming/example-pixel-art/5355812004872192">code</a>.</p>
<script src="https://cdn.jsdelivr.net/processing.js/1.4.8/processing.min.js"></script>
<script>
var sketchProc = function(processingInstance) {
with(processingInstance) {
size(400, 400);
frameRate(30);
// HOW TO USE
// Pick a grid size with the grid variable below
// Change the color variable below to whatever
// There is no eraser, becaue I couldn't figure out how to make it work.
var gridSize = 16;
var pixels = [];
var alreadyExists;
var color1 = document.getElementById('color1');
var color2 = document.getElementById('color2');
var color3 = document.getElementById('color3');
background(255, 255, 255);
for (var i = 0; i < gridSize; i++) {
stroke(0, 0, 0);
line(400 / gridSize * i, 0, 400 / gridSize * i, 400);
line(0, 400 / gridSize * i, 400, 400 / gridSize * i);
}
var update = function() {
background(255, 255, 255);
for (var i = 0; i < gridSize; i++) {
stroke(0, 0, 0);
line(400 / gridSize * i, 0, 400 / gridSize * i, 400);
line(0, 400 / gridSize * i, 400, 400 / gridSize * i);
}
for (var i = 0; i < pixels.length; i++) {
noStroke();
pushMatrix();
scale(0.5);
fill(pixels[i][2][0], pixels[i][2][1], pixels[i][2][2]);
rect(pixels[i][0] * (400 / gridSize), pixels[i][1] * (400 / gridSize), 400 / gridSize, 400 / gridSize);
popMatrix();
}
document.getElementById("output").innerHTML = pixels;
};
var mousePressed = mouseDragged = function() {
alreadyExists = false;
closestPixel = [ceil(mouseX / (400 / gridSize)) - 1, ceil(mouseY / (400 / gridSize)) - 1, [color1.value, color2.value, color3.value]];
for (var i = 0; i < pixels.length; i++) {
if (samePixels(pixels[i], closestPixel)) {
alreadyExists = true;
pixels[i][2] = [color1.value, color2.value, color3.value];
break;
}
}
if (!alreadyExists) {
pixels.push(closestPixel);
}
update();
};
var samePixels = function(p1, p2) {
var samePosition = p1[0] === p2[0] && p1[1] === p2[1];
if (!samePosition) {
return false;
}
return true;
};
}
};
var canvas = document.getElementById("mycanvas");
// Pass the function sketchProc (defined in myCode.js) to Processing's constructor.
var processingInstance = new Processing(canvas, sketchProc);
</script>
<p id="output" style="color:red">This is where the output will appear.</p>
</body>
</html>
As the title states, a strange visual bug keeps happening with my pixel art program. When I scale down using pushMatrix()
, scale()
and popMatrix()
. I can't really describe the bug, so I got a screenshot of it (the bug is the strange white lines in between pixels).
Code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Pixel Art Drawer & Importer</title>
</head>
<body>
<canvas id="mycanvas"></canvas>
<input type="text" id="color1" value="100" maxlength="3">
<input type="text" id="color2" value="100" maxlength="3">
<input type="text" id="color3" value="100" maxlength="3">
<p>You can import the pixel art textures into a game or program with this <a href="https://www.khanacademy.org/computer-programming/example-pixel-art/5355812004872192">code</a>.</p>
<script src="https://cdn.jsdelivr.net/processing.js/1.4.8/processing.min.js"></script>
<script>
var sketchProc = function(processingInstance) {
with(processingInstance) {
size(400, 400);
frameRate(30);
// HOW TO USE
// Pick a grid size with the grid variable below
// Change the color variable below to whatever
// There is no eraser, becaue I couldn't figure out how to make it work.
var gridSize = 16;
var pixels = [];
var alreadyExists;
var color1 = document.getElementById('color1');
var color2 = document.getElementById('color2');
var color3 = document.getElementById('color3');
background(255, 255, 255);
for (var i = 0; i < gridSize; i++) {
stroke(0, 0, 0);
line(400 / gridSize * i, 0, 400 / gridSize * i, 400);
line(0, 400 / gridSize * i, 400, 400 / gridSize * i);
}
var update = function() {
background(255, 255, 255);
for (var i = 0; i < gridSize; i++) {
stroke(0, 0, 0);
line(400 / gridSize * i, 0, 400 / gridSize * i, 400);
line(0, 400 / gridSize * i, 400, 400 / gridSize * i);
}
for (var i = 0; i < pixels.length; i++) {
noStroke();
pushMatrix();
scale(0.5);
fill(pixels[i][2][0], pixels[i][2][1], pixels[i][2][2]);
rect(pixels[i][0] * (400 / gridSize), pixels[i][1] * (400 / gridSize), 400 / gridSize, 400 / gridSize);
popMatrix();
}
document.getElementById("output").innerHTML = pixels;
};
var mousePressed = mouseDragged = function() {
alreadyExists = false;
closestPixel = [ceil(mouseX / (400 / gridSize)) - 1, ceil(mouseY / (400 / gridSize)) - 1, [color1.value, color2.value, color3.value]];
for (var i = 0; i < pixels.length; i++) {
if (samePixels(pixels[i], closestPixel)) {
alreadyExists = true;
pixels[i][2] = [color1.value, color2.value, color3.value];
break;
}
}
if (!alreadyExists) {
pixels.push(closestPixel);
}
update();
};
var samePixels = function(p1, p2) {
var samePosition = p1[0] === p2[0] && p1[1] === p2[1];
if (!samePosition) {
return false;
}
return true;
};
}
};
var canvas = document.getElementById("mycanvas");
// Pass the function sketchProc (defined in myCode.js) to Processing's constructor.
var processingInstance = new Processing(canvas, sketchProc);
</script>
<p id="output" style="color:red">This is where the output will appear.</p>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来像该程序的情况做您告诉它要做的事情,而不是您希望它要做:)
pushmatrix() / scale() / popmatrix()< / code>您使用的部分适用于如何渲染每个“像素”块(包括您注意到的中风),但不会影响任何鼠标光标逻辑。
一个选项是仅仅是
rect()
x,y值和大小的一半:例如,
请注意以上只是 loop的相关
in
update> update> update()< /code>:希望这个想法能清楚地说明。
您还可以在整个代码中考虑此量表,包括在鼠标Intedation中(可以受到约束):
Looks like a case of the program doing what you tell it to do, not you want it to do :)
The
pushMatrix() / scale() / popMatrix()
part you're using applies to how each "pixel" block is rendered (including the stroke which is what you're noticing), but doesn't affect any of the mouse cursor logic.One option would be to simply half the
rect()
x, y values and size:e.g.
Note the above is just the relevant
for
loop inupdate()
: hopefully the idea is clearly illustrated.You can also take this scale into account throughout the code, including in the mouse interation (which can be constrained):