画布 JavaScript 问题

发布于 2024-11-01 19:15:25 字数 978 浏览 0 评论 0原文

我被要求开发一个将在黑莓上运行的应用程序。但我不懂java。由于应用程序需要绘图,所以我选择了 html5 和 javascript。然后我读了一些javascript教程。但是当我尝试将其付诸实践时,我收到错误消息,指出“getContext”属性未定义。 可以用c#写吗?

var canvasCircle;
var contextCircle;
var x = 400;
var y = 300;
var dx = 2;
var WIDTH = 800;
var HEIGHT = 600;

// the circle wont make any transsformation.
function draw_circle(x, y, r) {
    contextCircle.beginPath();
    contextCircle.arc(x, y, r, 0, 2 * Math.PI, true);
    contextCircle.closePath();
    contextCircle.stroke();
}

function clear_canvas() {
    contextCircle.clearRect(0, 0, WIDTH, HEIGHT);
}

function init() {
    canvasCircle = document.getElementById("canvas_circle");
    contextCircle = canvasCircle.getContext('2d');
    return setInterval(draw, 10);
}

function draw() {
    clear_canvas();
    draw_circle(x, y, 50);

    if (x + dx > WIDTH || x + dx < 0)
        dx = -dx;
    x += dx;
}
init();

<canvas id="canvas_circle" width="800" height="600"></canvas>

I am asked to develop an application that will run on Blackberry. But I don't knowledge in java. Since the application needs drawing, I opt for html5 and javascript. Then I read some javascript tutorials. But when I try to put it into practice it, I get error saying that the "getContext" attribut is undefined.
Is it possible to write it in c#?

var canvasCircle;
var contextCircle;
var x = 400;
var y = 300;
var dx = 2;
var WIDTH = 800;
var HEIGHT = 600;

// the circle wont make any transsformation.
function draw_circle(x, y, r) {
    contextCircle.beginPath();
    contextCircle.arc(x, y, r, 0, 2 * Math.PI, true);
    contextCircle.closePath();
    contextCircle.stroke();
}

function clear_canvas() {
    contextCircle.clearRect(0, 0, WIDTH, HEIGHT);
}

function init() {
    canvasCircle = document.getElementById("canvas_circle");
    contextCircle = canvasCircle.getContext('2d');
    return setInterval(draw, 10);
}

function draw() {
    clear_canvas();
    draw_circle(x, y, 50);

    if (x + dx > WIDTH || x + dx < 0)
        dx = -dx;
    x += dx;
}
init();

<canvas id="canvas_circle" width="800" height="600"></canvas>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难如初 2024-11-08 19:15:25

我不熟悉您使用的 Blackberry 浏览器,但它似乎不支持 Canvas。 Dive Into HTML5 有一个关于功能检测的精彩章节:http://diveintohtml5.ep.io/detect.html< /a>

它还有一个名为 Modernizr 的不错的库,您可以使用它来检测 HTML5 功能。但检查画布支持非常简单(如本章所示):

 function supports_canvas() {
     return !!document.createElement('canvas').getContext;
 }

I'm not familiar with the Blackberry browser you are using, but it looks like it doesn't support Canvas. Dive Into HTML5 has a great chapter on feature detection: http://diveintohtml5.ep.io/detect.html

It also has a nice library called Modernizr you can use to detect HTML5 features. But to check for canvas support is pretty simple (as the chapter shows):

 function supports_canvas() {
     return !!document.createElement('canvas').getContext;
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文