canvas.getContext(“2d”)未定义 [英] canvas.getContext("2d") is undefined

查看:253
本文介绍了canvas.getContext(“2d”)未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码在所有浏览器都打开,但是当我试图运行WPF Web浏览器控件中的代码,它给出一个Javascript错误canvas.getContext(2d)未定义。

The below code opens fine in all browsers, but when I'm trying to run the code in WPF web browser control, it gives a Javascript error "canvas.getContext("2d") is undefined".

<html>
   <head>
      <title>Bar Chart</title>
      <script src="Chart.js" type="text/javascript"></script>  
      <script type="text/javascript">
       var canvas = null;
       var context = null;

       window.onload = function () {
        invokeService();
        canvas = document.getElementById("canvas");
        var context = canvas.getContext("2d");
        alert(context);
        var myLine = new Chart(document.getElementById("canvas").getContext("2d")).Bar(barChartData);
    };

    var barChartData;
    function invokeService() {
        alert("q");
        barChartData = {
            labels: ["January", "February", "March", "April", "May", "June", "July"],
            datasets: [
                       {
                           fillColor: "rgba(220,220,220,0.5)",
                           strokeColor: "rgba(220,220,220,1)",
                           data: [65, 59, 90, 81, 56, 55, 40]
                       },
                       {
                           fillColor: "rgba(151,187,205,0.5)",
                           strokeColor: "rgba(151,187,205,1)",
                           data: [28, 48, 40, 19, 96, 27, 100]
                       }
                 ]
        }
    }
    </script>  
 </head>
 <body>
     <canvas id="canvas" height="450" width="600"></canvas>
 </body>
</html>

编辑1:可以在此下载chart.js https://github.com/nnnick/Chart.js

Edit 1: chart.js can be download for here https://github.com/nnnick/Chart.js

推荐答案

在一杯咖啡后,我通过添加以下行解决了这个问题。

After one cup of coffee I have solved that issue by adding following line

<meta http-equiv="X-UA-Compatible" content="IE=9">

这篇关于canvas.getContext(“2d”)未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆