gpt4 book ai didi

javascript - 为什么我的 javascript canvas 绘图功能失控了?

转载 作者:行者123 更新时间:2023-12-02 22:12:11 25 4
gpt4 key购买 nike

Canvas 迭代器应该在 Canvas 上随机化黄色和绿色之间的 20px^2 正方形,但由于某种未知的原因,像素比正方形宽,直到大约穿过 Canvas 的一半,其中像素碎片成非常高的列。

预期的结果是让 Canvas 填充由随机黄色或绿色的 20x20 像素正方形组成的屏幕, Canvas 会自动缩放以更改大小和纵横比以适应窗口大小的调整。

实际结果是 Canvas 似乎产生了屏幕的三分之一作为非常短和宽的像素,具有预期的颜色图案,下面是非常高的列,然后是所有这些后面的页面。

我已经在其他地方询问过,但没有结果,我正在寻找任何能够满足预期目标的解决方案,所以我对任何事情都持开放态度。

Source replit片段:

var has = false
var hasLoaded = false

console.log(window.innerHeight)
console.log(window.innerWidth)
console.log(window.innerWidth*1.5)



var has = false
function toggle() {
console.log("" + has.toString())
if(has){
has = false
document.getElementById('cx').classList.remove('filtered')
}else{
has = true
document.getElementById('cx').classList.add('filtered')
}
}

function resize() {
//window resize event to find canvas size
var h = window.innerHeight
var w = window.innerWidth
var p = 20
var ph = Math.floor(h/p)
var pw = Math.floor(w/p)
let refcx = document.getElementById('cx')
refcx.width = pw
console.log("resized")
dynDraw(w, h, pw, ph, p);
}


function dynDraw(w, h, pw, ph, p) {
//wha
//for drawing after resize event

console.log(w + "w, "+h+"h, "+pw+"pw, "+ph+"ph")
var c = document.getElementById("cx");
var ctx = c.getContext("2d");

ctx.imageSmoothingEnabled = false
//ctx.fillStyle = "#ff0000"
//ctx.fillRect(20, 20, 150, 100);
ctx.fillStyle = "#00ff00"

for(let row = 0; row <= ph; row++)
{ //each row accross width lines, ph stacks
for(let i = 0; i <= pw; i++)
{ //each pixel across height lines, pw stacks
Math.random() >= 0.5 ? ctx.fillStyle = "#fff000" : ctx.fillStyle = "#00ff00"
ctx.fillRect(i, row, pw, pw)
console.log(i + "," + row + "," + pw + "," + ph)
}
}
console.log("done")
}
#cx {
position: fixed;
top: 0px;
left: 0px;

height: 100vh;
width: 100vw;

z-index: -5;
}
.filtered {
image-rendering: -moz-crisp-edges;
image-rendering: -webkit-crisp-edges;
image-rendering: pixelated;
image-rendering: crisp-edges;
}
<!DOCTYPE html>

<html>

<head>

<meta charset = "utf-8">
<meta name = "viewport" content = "width = device-width">
<title>Canvas Testing</title>
<link href = "style.css" rel = "stylesheet"></link>

<script src = "script.js"></script>

</head>

<body onload="resize();" >
<button id="buttonToDoStuff" onclick="toggle();">toggle filter</button>
<canvas id="cx" width="100" height="100">

Your browser does not support the HTML5 canvas tag.

</canvas>

</body>

</html>

the canvas

最佳答案

这一行

ctx.fillRect(i, row, pw, pw)

应该是

ctx.fillRect(i, row, 1, 1)

相反。

您还需要设置 widthheightresize处理程序。您只需设置width .

问题是因为您已经调整了 Canvas 绘制区域(上下文)的大小,使得您想要的每个正方形相对于上下文尺寸都是 1x1。 <canvas>widthheight属性控制 Canvas 元素和 Canvas 绘图上下文尺寸。 CSS widthheight覆盖元素尺寸,但不影​​响绘图上下文尺寸,而是拉伸(stretch)绘图上下文以适合元素。

var has = false
var hasLoaded = false

console.log(window.innerHeight)
console.log(window.innerWidth)
console.log(window.innerWidth*1.5)



var has = false
function toggle() {
console.log("" + has.toString())
if(has){
has = false
document.getElementById('cx').classList.remove('filtered')
}else{
has = true
document.getElementById('cx').classList.add('filtered')
}
}

function resize() {
//window resize event to find canvas size
var h = window.innerHeight
var w = window.innerWidth
var p = 20
var ph = Math.floor(h/p)
var pw = Math.floor(w/p)
let refcx = document.getElementById('cx')
refcx.width = pw
refcx.height = ph
console.log("resized")
dynDraw(w, h, pw, ph, p);
}


function dynDraw(w, h, pw, ph, p) {
//wha
//for drawing after resize event

console.log(w + "w, "+h+"h, "+pw+"pw, "+ph+"ph")
var c = document.getElementById("cx");
var ctx = c.getContext("2d");
ctx.imageSmoothingEnabled = false
//ctx.fillStyle = "#ff0000"
//ctx.fillRect(20, 20, 150, 100);
ctx.fillStyle = "#00ff00"

for(let row = 0; row <= ph; row++)
{ //each row accross width lines, ph stacks
for(let i = 0; i <= pw; i++)
{ //each pixel across height lines, pw stacks
Math.random() >= 0.5 ? ctx.fillStyle = "#fff000" : ctx.fillStyle = "#00ff00"
ctx.fillRect(i, row, 1, 1)
console.log(i + "," + row + "," + pw + "," + ph)
}
}
console.log("done")
}
#cx {
position: fixed;
top: 0px;
left: 0px;

height: 100vh;
width: 100vw;

z-index: -5;
}
.filtered {
image-rendering: -moz-crisp-edges;
image-rendering: -webkit-crisp-edges;
image-rendering: pixelated;
image-rendering: crisp-edges;
}
<!DOCTYPE html>

<html>

<head>

<meta charset = "utf-8">
<meta name = "viewport" content = "width = device-width">
<title>Canvas Testing</title>
<link href = "style.css" rel = "stylesheet"></link>

<script src = "script.js"></script>

</head>

<body onload="resize();" >
<button id="buttonToDoStuff" onclick="toggle();">toggle filter</button>
<canvas id="cx">

Your browser does not support the HTML5 canvas tag.

</canvas>

</body>

</html>

关于javascript - 为什么我的 javascript canvas 绘图功能失控了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59524946/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com