简体   繁体   English

无法在JavaScript中调用绘图功能

[英]Cannot call draw function in javascript

I have a project creating layouts and i have decided to do it with canvas element.I created a function that takes 4 args. 我有一个创建布局的项目,我决定使用canvas element.I创建了一个需要4个参数的函数。

   function fillArc(camvas,x,y,w,h)
{
canv.beginPath();
    var canv = document.getElementById("camvas");
    var context = canv.getContext('2d');
    context.strokeStyle="#FFFFFF";
    context.moveTo(x+5,y);
    context.lineTo(w-5,y);
    context.quadraticCurveTo(w,y,w,y+5);
    context.lineTo(w,h-5);
    context.quadraticCurveTo(w,h,w-5,h);
    context.lineTo(x+5,h);  
    context.quadraticCurveTo(x,h,x,h-5);
    context.lineTo(x,y+5);
    context.quadraticCurveTo(x,y,x+5,y);
    context.stroke();
canv.closePath();


}

I have several canvas elements so i want to create this border-radius box in different areas.I assumed that a call like: 我有几个画布元素,所以我想在不同的区域创建这个border-radius框。我假设这样的调用:

<canvas width="500" height="500" id="canvaslayouts">
</canvas>

<script>
fillArc(canvaslayouts,10,10,50,50);
</script>

But this doesn't seem to work.Can anyone point my mistake please. 但这似乎不起作用,任何人都可以指出我的错误。

Putting it in a <script> block should do the trick: 将其放在<script>块中应该可以解决问题:

<canvas width="500" height="500" id="canvaslayouts"></canvas>
<script>
fillArc(canvaslayouts,10,10,50,50);
</script>

Also, note that your fillArc() function definition (or any other executable JavaScript that you may have) should also be in a <script> block. 另外,请注意,您的fillArc()函数定义(或您可能拥有的任何其他可执行JavaScript)也应位于<script>块中。

You need to change the .beginPath() and .closePath() calls so that they're applied to the context , and not to the canvas . 您需要更改.beginPath().closePath()调用,以便将它们应用于上下文 ,而不是画布

That can of course only be done once you've called canv.getContext() . 当然,只有在调用canv.getContext()才能完成此操作。

You also need to: 您还需要:

  1. use the correct ID - you've used "camvas" when it should be the variable with that name 使用正确的ID-您应该使用"camvas"作为其名称的变量
  2. put your JS inside <script> tags 将您的JS放入<script>标记中

ie: 即:

<script>
function fillArc(camvas,x,y,w,h)
{
    var canv = document.getElementById(camvas); 
    var context = canv.getContext('2d');
    context.beginPath();
    context.strokeStyle="#FFFFFF";
    context.moveTo(x+5,y);
    context.lineTo(w-5,y);
    context.quadraticCurveTo(w,y,w,y+5);
    context.lineTo(w,h-5);
    context.quadraticCurveTo(w,h,w-5,h);
    context.lineTo(x+5,h);  
    context.quadraticCurveTo(x,h,x,h-5);
    context.lineTo(x,y+5);
    context.quadraticCurveTo(x,y,x+5,y);
    context.closePath();  // NB: close the path before you stroke it
    context.stroke();
}
</script>

<canvas width="500" height="500" id="canvaslayouts"> </canvas>
<script>
    fillArc("canvaslayouts", 10, 10, 50, 50);
</script>    

Well, a few things: 好吧,几件事:

  1. You're never using the camvas variable. 您永远不会使用camvas变量。
  2. You're always refering to #camvas . 您总是#camvas
  3. You call canv.beginPath() before setting canv to anything. 您可以在将canv设置为任何值之前调用canv.beginPath()
  4. Your javascript code in the canvas is not in script tags 您在画布中的javascript代码不在脚本标签中
  5. Your javascript code in the canvas tag calls the function with canvaslayouts as the first parameter. canvas标记中的javascript代码会调用以canvaslayouts作为第一个参数的函数。 This means you're searching for a variable named "canvaslayouts" and not an ID. 这意味着您正在搜索名为“ canvaslayouts”的变量,而不是ID。 Change it to "canvaslayouts" to specify that it's a string to be used in getElementById . 将其更改为"canvaslayouts"以指定它是要在getElementById使用的字符串。

The correct code should be something like this: 正确的代码应如下所示:

function fillArc(camvas,x,y,w,h)
{
    //Changed to use the variable instead, and moved it to the start
    var canv = document.getElementById(camvas); 
    canv.beginPath();
        var context = canv.getContext('2d');
        context.strokeStyle="#FFFFFF";
        context.moveTo(x+5,y);
        context.lineTo(w-5,y);
        context.quadraticCurveTo(w,y,w,y+5);
        context.lineTo(w,h-5);
        context.quadraticCurveTo(w,h,w-5,h);
        context.lineTo(x+5,h);  
        context.quadraticCurveTo(x,h,x,h-5);
        context.lineTo(x,y+5);
        context.quadraticCurveTo(x,y,x+5,y);
        context.stroke();
    canv.closePath();
}

and

<canvas width="500" height="500" id="canvaslayouts">
    <script type="text/javascript">
        //Use a script container
        fillArc("canvaslayouts",10,10,50,50); //Use a string, not a variable
    </script>
</canvas>

jsFiddle Demo

  • Place your function and the call to it in a script element 将函数及其调用放置在script元素中
  • Pass in a string which matches the id of the canvas you wish to draw on 传递与您要绘制的画布的id相匹配的字符串
  • Use the context, not the canvas, to access the drawing API 使用上下文而不是画布来访问绘图API

     <script> function fillArc(camvas,x,y,w,h) { var canv = document.getElementById(camvas); var context = canv.getContext('2d'); context.beginPath(); context.strokeStyle="#ffffff"; context.moveTo(x+5,y); context.lineTo(w-5,y); context.quadraticCurveTo(w,y,w,y+5); context.lineTo(w,h-5); context.quadraticCurveTo(w,h,w-5,h); context.lineTo(x+5,h); context.quadraticCurveTo(x,h,x,h-5); context.lineTo(x,y+5); context.quadraticCurveTo(x,y,x+5,y); context.stroke(); context.closePath(); } fillArc("canvaslayouts",10,10,50,50); </script> 
  • 声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

     
    粤ICP备18138465号  © 2020-2024 STACKOOM.COM