javascript - 使用 jquery 创建了一个简单的 BREAKOUT 游戏,尝试转换回纯 JavaScript

标签 javascript jquery html jsfiddle breakout

我使用一些 jquery 和一个方便的在线教程创建了突破。

这是 fiddle :http://jsfiddle.net/Kinetic915/9bLEk/6/

我成功更改了访问窗口大小的宽度和高度,如下所示:

来自:

WIDTH = $("#canvas")[0].width = $(window).width();
HEIGHT = $("#canvas")[0].height = $(window).height();

致:

var WIDTH  = document.getElementById("canvas");
var HEIGHT  = document.getElementById("canvas");

WIDTH.width = window.innerWidth;
HEIGHT.height = window.innerHeight;
WIDTH = window.innerWidth;
HEIGHT = window.innerHeight;

代码正确加载。我发现更改渲染球的代码时出现问题。
它在这里与 jquery 一起使用:

Cir = $('#canvas')[0].getContext("2d");

Cir.beginPath();
Cir.arc(x, y, 10, 0, Math.PI * 2, true);
Cir.closePath();
Cir.fill();

不适用于:

var canvas = document.getElementById("canvas");
var Cir = canvas.getContext("2d");

Cir.beginPath();
Cir.arc(x, y, 10, 0, Math.PI * 2, true);
Cir.closePath();
Cir.fill();

有什么建议吗?

最佳答案

将其更改为:

var canvas = document.getElementById("canvas");
var Cir = canvas.get(0).getContext("2d");

关于javascript - 使用 jquery 创建了一个简单的 BREAKOUT 游戏,尝试转换回纯 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16426616/

相关文章:

javascript - 使用属性 "title"将 html 元素链接到 jQuery.click()

javascript - 使用 vw 和 vh 度量调整窗口大小的问题

javascript - .innerHTML 应用于两个 div,而应仅应用于一个

jquery - Foundation 粘性菜单链接在 Google Chrome 中不起作用

html - 将鼠标悬停在 div 上时显示不同的图像

javascript - 当我的导航栏折叠时如何使按钮起作用?

javascript - javascript 是否可以引用创建它的脚本元素?

Javascript/AJAX/jQuery - 对象预期行 1 仅在 IE 中

javascript - 为什么所需的 knockout 验证最初不起作用?

javascript - 有条件地加载图像以进行响应式设计