最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - HTMLCanvasElement has no method 'toDataUrl' - Stack Overflow

programmeradmin6浏览0评论

I am trying to fetch the dataUrl from the canvas to use is as css background-image on various elements. But i always get following error Uncaught TypeError: Object #<HTMLCanvasElement> has no method 'toDataUrl'

this is my test code

<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #c3c3c3;">
Your browser does not support the canvas element.
</canvas>
<script type="text/javascript">
  var c=document.getElementById("myCanvas");
  var ctx=c.getContext("2d");
  ctx.fillStyle="#FF0000";
  ctx.fillRect(0,0,150,75);
  alert(c.toDataUrl());
</script>
</body>
</html>

is it once again the security feature in disguise?, or am i simply stupid...

Thanks in advance

I am trying to fetch the dataUrl from the canvas to use is as css background-image on various elements. But i always get following error Uncaught TypeError: Object #<HTMLCanvasElement> has no method 'toDataUrl'

this is my test code

<!DOCTYPE html>
<html>
<body>
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #c3c3c3;">
Your browser does not support the canvas element.
</canvas>
<script type="text/javascript">
  var c=document.getElementById("myCanvas");
  var ctx=c.getContext("2d");
  ctx.fillStyle="#FF0000";
  ctx.fillRect(0,0,150,75);
  alert(c.toDataUrl());
</script>
</body>
</html>

is it once again the security feature in disguise?, or am i simply stupid...

Thanks in advance

Share Improve this question edited Apr 22, 2012 at 13:18 Dennis 32.6k12 gold badges65 silver badges79 bronze badges asked Apr 22, 2012 at 12:59 ValerijValerij 27.7k1 gold badge29 silver badges43 bronze badges 3
  • 3 You can find the Mozilla Documentation Network pages for stuff like this with a google search for "MDN toDataUrl" - google is case-insensitive. – Pointy Commented Apr 22, 2012 at 13:04
  • Read the docs. Any docs. w3/TR/html5/the-canvas-element.html#dom-canvas-todataurl – Matt Ball Commented Apr 22, 2012 at 13:05
  • this is what happens when you code for 20 hours straight – Valerij Commented Apr 22, 2012 at 13:18
Add a ment  | 

1 Answer 1

Reset to default 20

You have the function name incorrect. Watch the case:

alert(c.toDataURL());

DEMO

发布评论

评论列表(0)

  1. 暂无评论