gpt4 book ai didi

javascript - Canvas 消失

转载 作者:太空宇宙 更新时间:2023-11-04 14:01:35 27 4
gpt4 key购买 nike

我用 JS 写了一个简单的脚本。当我将它放入 html 文档(在 body 标签的底部)时,它工作正常。当我尝试将它放入外部文档并链接它时,脚本运行但 canvas 元素消失(刚刚检查:当我将脚本放在底部而不是 head 标签时它有效)。这个版本有效:

			var c = document.getElementById("canvasId");
var ctx = c.getContext("2d");
var text = "Nazwa pomiaru";
var boxWidth = 200;
var boxHeight = 200;
var minValue = 0;
var maxValue = 100;
var midValue = (Math.abs(minValue) + Math.abs(maxValue))/2;
var unit = "%";
var indicWidth = 50;
var indicHeight = 0.6 * boxHeight;
var textPosition = 0.875;
var leftMargin = 0.375;
var topMargin = 0.15;
var bottomMargin = 0.75;
var value = Math.floor(Math.random() * 101);
var warningTop = 90;
var dangerTop = 95;
var warningBottom = 10;
var dangerBottom = 5;
var thresholdTrasparency = 0.6;
var strokeColor = "#CCCCCC";
var valueColor = "#993333";
var warningColor = "#ff6600";
var dangerColor = "#cc0000";
var textColor = "#000000";
var textColor2 = "#ffffff";
ctx.beginPath();
ctx.fillText((maxValue + unit), (leftMargin * boxWidth - ctx.measureText(maxValue + unit).width-1), (topMargin * boxHeight + 3), (boxWidth / 4)); //draw a maxValue
ctx.fillText((midValue + unit), (leftMargin * boxWidth - ctx.measureText(midValue + unit).width-1), ((topMargin + bottomMargin) / 2 * boxHeight + 3), (boxWidth / 4)); //draw a midValue
ctx.fillText((minValue + unit), (leftMargin * boxWidth - ctx.measureText(minValue + unit).width-1), (bottomMargin * boxHeight + 3), (boxWidth / 4)); //draw a minValue
ctx.fillText(text, ((boxWidth - ctx.measureText(text).width) / 2), (textPosition * boxHeight), (textPosition * boxWidth)); //type a text
ctx.lineWidth = "0";
ctx.globalAlpha = thresholdTrasparency;
if (warningTop !== 0 || warningBottom !== 0) {
ctx.fillStyle = warningColor;
ctx.fillRect((leftMargin * boxWidth), (topMargin * boxHeight), indicWidth, ((100 - warningTop) * (indicHeight / 100))); //draw a warningTop
ctx.fillRect((leftMargin * boxWidth), (topMargin * boxHeight + (100 - warningBottom) * (indicHeight / 100)), indicWidth, (indicHeight - (100 - warningBottom) * (indicHeight / 100))); //draw a warningBottom
}
if (dangerTop !== 0 || dangerBottom !== 0) {
ctx.fillStyle = dangerColor;
ctx.fillRect((leftMargin * boxWidth), (topMargin * boxHeight), indicWidth, ((100 - dangerTop) * (indicHeight / 100))); //draw a dangerTop
ctx.fillRect((leftMargin * boxWidth), (topMargin * boxHeight + (100 - dangerBottom) * (indicHeight / 100)), indicWidth, (indicHeight - (100 - dangerBottom) * (indicHeight / 100))); //draw a dangerBottom
}
ctx.beginPath();
ctx.globalAlpha = 1;
ctx.fillStyle = valueColor;
ctx.fillRect((leftMargin * boxWidth), (topMargin * boxHeight + (100 - value) * (indicHeight / 100)), indicWidth, (indicHeight - (100 - value) * (indicHeight / 100))); //draw a valueIndicator
if (value <= 47) {
ctx.fillStyle= textColor;
}
else {
ctx.fillStyle = textColor2;
}
ctx.fillText(value, ((boxWidth - ctx.measureText(value).width) / 2), (boxHeight / 2));
ctx.shadowBlur = 6;
ctx.shadowColor = "#000000";
ctx.lineWidth="2";
ctx.strokeStyle = strokeColor;
ctx.strokeRect((leftMargin * boxWidth), (topMargin * boxHeight), indicWidth, indicHeight); //draw a rectangle
ctx.shadowBlur = 0;
document.write(value);
<!doctype html>
<html>
<head>
<title>Slider</title>
</head>
<body>
<canvas id="canvasId" style="border: 3px solid; background: #ffffff;" width="200px" height="200px">
No support.
</canvas>
<script src="canvas.js"></script>
</body>
</html>

这个版本不工作:

function draw () {
var c = document.getElementById("canvasId");
var ctx = c.getContext("2d");
var text = "Nazwa pomiaru";
var boxWidth = 200;
var boxHeight = 200;
var minValue = 0;
var maxValue = 100;
var midValue = (Math.abs(minValue) + Math.abs(maxValue))/2;
var unit = "%";
var indicWidth = 50;
var indicHeight = 0.6 * boxHeight;
var textPosition = 0.875;
var leftMargin = 0.375;
var topMargin = 0.15;
var bottomMargin = 0.75;
var value = Math.floor(Math.random() * 101);
var warningTop = 90;
var dangerTop = 95;
var warningBottom = 10;
var dangerBottom = 5;
var thresholdTrasparency = 0.6;
var strokeColor = "#CCCCCC";
var valueColor = "#993333";
var warningColor = "#ff6600";
var dangerColor = "#cc0000";
var textColor = "#000000";
var textColor2 = "#ffffff";
ctx.beginPath();
ctx.fillText((maxValue + unit), (leftMargin * boxWidth - ctx.measureText(maxValue + unit).width-1), (topMargin * boxHeight + 3), (boxWidth / 4)); //draw a maxValue
ctx.fillText((midValue + unit), (leftMargin * boxWidth - ctx.measureText(midValue + unit).width-1), ((topMargin + bottomMargin) / 2 * boxHeight + 3), (boxWidth / 4)); //draw a midValue
ctx.fillText((minValue + unit), (leftMargin * boxWidth - ctx.measureText(minValue + unit).width-1), (bottomMargin * boxHeight + 3), (boxWidth / 4)); //draw a minValue
ctx.fillText(text, ((boxWidth - ctx.measureText(text).width) / 2), (textPosition * boxHeight), (textPosition * boxWidth)); //type a text
ctx.lineWidth = "0";
ctx.globalAlpha = thresholdTrasparency;
if (warningTop !== 0 || warningBottom !== 0) {
ctx.fillStyle = warningColor;
ctx.fillRect((leftMargin * boxWidth), (topMargin * boxHeight), indicWidth, ((100 - warningTop) * (indicHeight / 100))); //draw a warningTop
ctx.fillRect((leftMargin * boxWidth), (topMargin * boxHeight + (100 - warningBottom) * (indicHeight / 100)), indicWidth, (indicHeight - (100 - warningBottom) * (indicHeight / 100))); //draw a warningBottom
}
if (dangerTop !== 0 || dangerBottom !== 0) {
ctx.fillStyle = dangerColor;
ctx.fillRect((leftMargin * boxWidth), (topMargin * boxHeight), indicWidth, ((100 - dangerTop) * (indicHeight / 100))); //draw a dangerTop
ctx.fillRect((leftMargin * boxWidth), (topMargin * boxHeight + (100 - dangerBottom) * (indicHeight / 100)), indicWidth, (indicHeight - (100 - dangerBottom) * (indicHeight / 100))); //draw a dangerBottom
}
ctx.beginPath();
ctx.globalAlpha = 1;
ctx.fillStyle = valueColor;
ctx.fillRect((leftMargin * boxWidth), (topMargin * boxHeight + (100 - value) * (indicHeight / 100)), indicWidth, (indicHeight - (100 - value) * (indicHeight / 100))); //draw a valueIndicator
if (value <= 47) {
ctx.fillStyle= textColor;
}
else {
ctx.fillStyle = textColor2;
}
ctx.fillText(value, ((boxWidth - ctx.measureText(value).width) / 2), (boxHeight / 2));
ctx.shadowBlur = 6;
ctx.shadowColor = "#000000";
ctx.lineWidth="2";
ctx.strokeStyle = strokeColor;
ctx.strokeRect((leftMargin * boxWidth), (topMargin * boxHeight), indicWidth, indicHeight); //draw a rectangle
ctx.shadowBlur = 0;
document.write(value);
};

window.onload = draw;
<!doctype html>
<html>
<head>
<title>Slider</title>
</head>
<body>
<canvas id="canvasId" style="border: 3px solid; background: #ffffff;" width="200px" height="200px">
No support.
</canvas>
<script src="canvas.js"></script>
</body>
</html>

第二个版本以某种方式隐藏了 Canvas 标签,但显示了文本(因此脚本有效)。

有什么解决办法吗?

最佳答案

Canvas (以及整个文档)消失的原因是因为您正在使用 document.write。在第一个片段中,您在 关闭流 之前写入文档。对于第二个片段,您仅在 window.onLoad 之后执行该代码,此时文档流已关闭。

Note: as document.write writes to the document stream, calling document.write on a closed (loaded) document automatically calls document.open which will clear the document. - https://developer.mozilla.org/en-US/docs/Web/API/Document/write

解决方案是:

1) 不要使用 document.write - 如果您需要写入文档,请在页面上放置一个 HTML 元素,然后写入该元素:

 // html:
<html>
...
<body>
<p id="output"></p>
</body>
</html>

// Javascript
// ...
var el = document.getDocumentById('output');
el.textContent = value

2) 不要在页面加载后调用 document.write(就像您在第一个片段中所做的那样)

就个人而言,我永远不会使用 document.write,因为您可能会遇到诸如您所描述的问题。

关于javascript - Canvas 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31179511/

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