简体   繁体   English

我试过在 JavaScript 中设置我的 Canvas 但它不起作用

[英]I've tried setting up my Canvas in JavaScript and it isn't working

I've restarted about 5 times and it's not working.我已经重新启动了大约 5 次,但没有用。 I'm trying to set up my canvas. The first part is the code in the js file and the second code is what I put in the HTML file.我正在尝试设置我的 canvas。第一部分是 js 文件中的代码,第二部分是我放入 HTML 文件中的代码。

JS:记者:

var canvas;
var ctx;
var w = 1000;
var h = 600;


function setUpCanvas(){
    canvas = document.querySelector("#myCanvas");
    ctx = canvas.getContext("2d");
    canvas.width = w;
    canvas.height = h;
    canvas.style.border = "5px dotted orange";
   

HTML: HTML:

 <!DOCTYPE html>
<html>
    <head>
        <link rel ="stylesheet" type = "text/css" href = "m4.css">
    </head>
    <body>
        <div id = "container">
            <h1> module 4 : arrays and loops</h1>
            <canvas id = "myCanvas"></canvas>
        </div>
        <script src = "m4.js"></script>
    </body>
</html>

Simple answer, it works.简单的答案,它的工作原理。 for proof, visit如需证明,请访问

https://loading.hermanboxcar5.repl.co https://loading.hermanboxcar5.repl.co

ok if you do ctrl u on that website it will give you the code you did (except my js file is named script.js ).好的,如果您在该网站上按 ctrl u,它会为您提供您所做的代码(除了我的 js 文件名为script.js )。 My best guess so far is after the last line of your js put a }到目前为止,我最好的猜测是在你的 js 的最后一行之后放一个}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的 javascript 不能重定向以下代码,我尝试了多种解决方案,我在 StackOverFlow 中找到了 - Why can't my javascript redirect the following code, I've tried multiple solution that i found in StackOverFlow HTML5 / Javascript-我的简单画布动画不起作用 - HTML5/Javascript - My simple canvas animation isn't working 我的 JavaScript 没有按预期工作,我该如何纠正和清理这个 XHR POST 请求? - My JavaScript isn't working as intended, how can I correct and clean up this XHR POST request? JavaScript classList不起作用。 我尝试了在这里看到的各种方法 - JavaScript classList is not working. I've tried various methods I've seen on here 为什么我的画布不能处理2个粒子? - Why isn't my canvas working with 2 particles? 为什么我的canvas clearRect无法正常工作? - Why isn't my canvas clearRect working? 按键不起作用Javascript - Key Up isn't working Javascript 为什么我的矩形没有出现在 HTML Canvas 中? - Why isn't my rectangle showing up in HTML Canvas? 为什么不能以短代码将Web Clip图标添加到我的Web应用程序中。 我已经尝试了一切? - Why can't I add a web clip icon to my webapp in dashcode. I've tried everything? 帆布屋。 将代码复制到编辑器并在浏览器中运行时,为什么代码不起作用? - Canvas- house. Why isn`t the code working, when I copy it to an editor and run it in my browser?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM