您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 濮阳分类信息网,免费分类信息发布

HTML5 Canvas的基本用法介绍

2024/5/15 18:06:53发布35次查看
本篇文章给大家带来的内容是关于html5 canvas的基本用法介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
canvas 是 html5 当中我最喜欢的所有新特性中我最喜欢的一个标签了。因为它太强大了,各种有意思的特效都可以实现。
1. canvas 的基本使用方法- 它是一个行内块元素
- 默认大小是 300 x 150,不能在 css 里给他设置样式,只能在标签内写它的属性。如 width = 400,height = 300
- 获取画布
   var canvas = document。queryselector(canvas)
- 获取画笔(上下文)
   var ctx = canvas.getcontext('2d')
2. canvas 绘制基本的图形填充矩形
ctx.fillrect(0,0,100,100)
fill:跟填充有关
rect: 描绘一个矩形
填充图形设置样式
ctx.fillstyle = 'green'
描边矩形
ctx.strokerect(100,100,100,100)
描边图形设置样式
ctx.strokestyle = 'white'
ctx.linewidth = 100
清除整个画布
ctx.clearrect(0,0,canvas.width,canvas.height)
画线段
ctx.moveto(100,100)
ctx.lineto(100,100)
描边
ctx.stroke()
填充
ctx.fill()-
起始点和结束点连接
ctx.closepath()
ctx.save()开头
   ......
ctx.restore()结尾
3. 画布时钟使用画布我们可以画一个时钟,包括刻度和时针,每一秒走的刻度可以用 data 对象通过定时器来时时更新。
var canvas = document.queryselector(canvas);    var ctx = canvas.getcontext(2d);    function move() {        ctx.save()            ctx.translate(300,300)            //  初始化一些公共的样式            ctx.linecap = 'round'            ctx.strokestyle = 'black'            ctx.linewidth = 8            ctx.scale(0.5,0.5)            // 画外面的圆            ctx.save();                ctx.beginpath();                ctx.strokestyle = 'gold';                ctx.arc(0,0,150,0,2*math.pi);                ctx.stroke();            ctx.restore();            // 画里面的刻度            ctx.save()                ctx.beginpath();                for (var i=0; i < 12; i++) {                    ctx.moveto(0,-125);                    ctx.lineto(0,-140);                    ctx.stroke()                    ctx.rotate(30*math.pi/180)                }            ctx.restore()            // 分针刻度            ctx.save()                ctx.linewidth = 3                for (var i = 0; i < 60; i++) {                    if (i % 5 != 0){                        ctx.beginpath()                        ctx.moveto(0,-135);                        ctx.lineto(0,-140);                        ctx.stroke()                    }                    ctx.rotate(6*math.pi/180)                }            ctx.restore()            // 当前时间            var date = new date()            var s = date.getseconds()            var min = date.getminutes() + s/60            var h = date.gethours() + min/60            // 时针            ctx.save()                ctx.rotate(30*h*math.pi/180)                ctx.linewidth = 14                ctx.beginpath()                ctx.moveto(0,-80)                ctx.lineto(0,20)                ctx.stroke()            ctx.restore()            // 分针            ctx.save()                ctx.linewidth = 10                ctx.rotate(6*min*math.pi/180)                ctx.beginpath()                ctx.rotate(-30*math.pi/180)                ctx.moveto(0,-120)                ctx.lineto(0,30)                ctx.stroke()            ctx.restore()            //秒针            ctx.save()                ctx.linewidth = 6                ctx.strokestyle = 'pink'                ctx.fillstyle = 'pink'                ctx.rotate(6*s*math.pi/180)                ctx.beginpath()                ctx.arc(0,0,10,0,2*math.pi)                ctx.fill()                ctx.beginpath()                ctx.moveto(0,-125)                ctx.lineto(0,30)                ctx.stroke()                ctx.beginpath()                ctx.arc(0,-135,10,0,2*math.pi)                ctx.stroke()            ctx.restore()        ctx.restore()    }    setinterval(function () {        ctx.clearrect(0,0,canvas.width,canvas.height)        move()    },1000)
静止的图像如下图。
刮刮卡效果使用 canvas 的图形合成的属性可以实现图片合成的效果。具体应用于刮刮卡。
globalcompositeoperation属性设置或返回如何将一个源(新的)图像绘制到目标(已有)的图像上
源图像 = 您打算放置到画布上的绘图
目标图像 = 您已经放置在画布上的绘图
var  canvas = document.queryselector(canvas)    var ctx = getctx()    log(ctx)    ctx.fillstyle = 'yellow'    ctx.fillrect(0,0,400,400)    ctx.globalcompositeoperation = 'destination-out';    // 鼠标按下    canvas.onmousedown = function (event) {        ctx.beginpath()        ctx.arc(event.clientx - canvas.offsetleft,event.clienty - canvas.offsettop,            20,0,2*math.pi)        ctx.fill()        // 鼠标移动        document.onmousemove = function (event) {            ctx.beginpath()            ctx.arc(event.clientx - canvas.offsetleft,event.clienty - canvas.offsettop,            20,0,2*math.pi)            ctx.fill()        }        // 鼠标抬起        document.onmouseup = function () {            document.onmousemove = document.onmouseup = null        }        return false    }
以上就是html5 canvas的基本用法介绍的详细内容。
濮阳分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录