如何为 html5 canvas 制作 jQuery 函数
How to make a jQuery function for html5 canvas
将一个简单的画布视为
$(document).ready(function(){
draw();
});
function draw() {
var canvas = document.getElementById("canvas");
if (canvas.getContext) {
var ctx = canvas.getContext("2d");
ctx.fillStyle = "rgb(200,0,0)";
ctx.fillRect (10, 10, 55, 50);
ctx.fillStyle = "rgba(0, 0, 200, 0.5)";
ctx.fillRect (30, 30, 55, 50);
}
}
如何将变量引入 jQuery 函数以使用定义的变量(例如颜色集(绘制多个画布。
实际上,我想用draw(variables)
提供的变量替换画布id及其选项(如颜色(,例如draw(canvas_id, color, ...)
。
示例:(用于在不同的 DOM 元素上创建多个画布(
function draw(ccc) {
var canvas = document.getElementById(ccc);
if (canvas.getContext) {
var ctx = canvas.getContext("2d");
ctx.fillStyle = "rgb(200,0,0)";
ctx.fillRect (10, 10, 55, 50);
ctx.fillStyle = "rgba(0, 0, 200, 0.5)";
ctx.fillRect (30, 30, 55, 50);
}
}
draw(canvas1);
draw(canvas2);
试试这个:
function draw(id, clr, fill) {
var canvas = document.getElementById(id);
if (canvas.getContext) {
var ctx = canvas.getContext("2d");
ctx.fillStyle = clr;
ctx.fillRect (fill);
}
}
这里有一种方法可以做到这一点:
function draw(colors) {
var canvas = document.getElementById("canvas");
if (canvas.getContext) {
var ctx = canvas.getContext("2d");
for(var i=0; i < colors.length; i ++){
ctx.fillStyle = colors[i];
ctx.fillRect (10*i, 10*i, 55, 50);
}
}
}
// define some colors in an array
var colors = ["rgb(200,0,0)","rgba(0, 0, 200, 0.5)","rgba(0, 128, 200, 0.5)"];
draw(colors);
编辑
这是jsfiddle示例
相关文章:
- 我怎样才能制作一个“;reverse preventDefault”;在jQuery中
- 使用next、prev按钮制作简单的jQuery图像滑块
- 用Jquery map和moment js制作一个简单的时间线
- 使用jQuery库Packey来制作卡片布局,我可以用导航链接重新订购
- 在Jquery中制作删除和回复按钮
- jQuery Orbit-如何制作随机幻灯片
- 使用JQuery制作KineticJS动画
- 制作jQuery高度百分比动画
- 如何制作jquery脚本,在表为空时隐藏表
- 如何为 html5 canvas 制作 jQuery 函数
- 在队列中制作 jQuery 动画
- 如何制作jquery旋转缩略图jquery图片库
- 尝试用HTML设计台球游戏来制作jQuery游戏-我的第一个项目
- 如何制作jquery.Tabs占用< light >名称代替href
- 制作jquery数据表来提取和使用从Rpc服务器返回的json的给定部分
- 制作jquery/js数组
- 如何在Jasmine单元测试中使用Sinon制作jQuery动画?
- 制作JQuery计算器,更改innerHTML,但什么都没发生
- 如何制作jQuery对象的副本
- 从头开始制作jquery旋转木马