画布消失

时间:2015-07-02 08:36:35

标签: javascript html5 canvas

我在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); 
&#13;
<!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>
&#13;
&#13;
&#13;

此版本不起作用:

&#13;
&#13;
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;
&#13;
<!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>
&#13;
&#13;
&#13;

第二个版本隐藏了canvas标签但文本出现(因此脚本可以工作)。

有没有解决方案?

3 个答案:

答案 0 :(得分:3)

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

  

注意:当document.write写入文档流时,在已关闭(加载)的文档上调用document.write会自动调用document.open来清除文档。     - 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,因为您可能遇到诸如您描述的问题。

答案 1 :(得分:3)

问题是覆盖所有身体的document.write(value)

为什么呢?

Source

  

注意:当document.write写入文档流时,在已关闭(加载)的文档上调用document.write会自动调用document.open来清除文档。

所以,在你的第二天,你正在window.onload呼叫document.open并清除文件。在您的第一个版本中,您不要将其称为onload

答案 2 :(得分:2)

  

注意:当document.write写入文档流时,调用   关闭(加载)文档上的document.write会自动调用   document.open将清除文档

来源:Document.write()

Why is document.write considered a “bad practice”?