gpt4 book ai didi

javascript - Canvas 不显示

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:48:19 32 4
gpt4 key购买 nike

我已经按照有关创建 Canvas 的教程进行操作,但是它不起作用,也没有在其上绘制矩形。是否有必要在 <head> 中包含脚本? ?任何帮助将不胜感激!

这是一个 JSFiddle用我的代码。

<!DOCTYPE html>
<html>
<head>
<title>Simple animations in HTML5</title>
</head>
<body>
<h2> Optical Illusion </h2>
<video id="illusion" width="640" height="480" controls>
<source src="Illusion_movie.ogg">
</video>

<div id="buttonbar">
<button onclick="changeSize()">Big/Small</button>
</div>

<p>
Watch the animation for 1 minute, staring at the centre of the image. Then look at something else near you.
For a few seconds everything will appear to distort.
Source: <a href="http://en.wikipedia.org/wiki/File:Illusion_movie.ogg">Wikipedia:Illusion movie</a>
</p>

<script>
var myVideo=document.getElementById("illusion");
var littleSize = false;
function changeSize()
{
myVideo.width = littleSize ? 800 : 400;
littleSize = !littleSize;//toggle boolean
}
</script>

<canvas id= "myCanvas " width= "500 " height= "500 ">
style="border:1px solid #000000;">
</canvas>

<script>
var canvas = document.getElementById("myCanvas");
var context = canvas.getContext("2d");
context.fillStyle = "blue";
context.fillRect(20, 50, 200, 100);
</script>
</body>
</html>

最佳答案

我已经清理了您的代码,您有一些空格导致了问题。

<罢工>此外,当您使用脚本标签时,请设置类型属性:

<script type="text/javascript">
// your code here
</script>

<罢工>

这是一个修正后的 fiddle :http://jsfiddle.net/8bK4y/

编辑:正如下面指出的,HTML5 Doctype 不需要 type 属性。

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

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