Canvas and use applications

Canvas


HTML5 canvas is among my favorite of all the new features in my favorite label of. Because it was too strong, and a variety of interesting effects can be achieved.

1. canvas using a method substantially

- 它是一个行内块元素
- 默认大小是 300 x 150,不能在 css 里给他设置样式,只能在标签内写它的属性。如 width = 400,height = 300
- 获取画布
    var canvas = document。querySelector("canvas")
- 获取画笔(上下文)
    var ctx = canvas.getContext('2d')
    
    

2. canvas draw basic graphics

填充矩形
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. Canvas Clock

Use canvas we can draw a clock, including scale and hour, every second walk of scale can be constantly updated by the timer with the Data object.

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)

Still image as FIG.

clipboard.png

Scratch effect

Use canvas graphics synthetic properties can achieve the effect of synthesized images. Specifically applied to scratch.
globalCompositeOperation property sets or returns how a source (new) image is rendered to the target (existing) on the image
of the source image = you intend to place the drawing on the canvas
object image = You have to place on the canvas drawing

clipboard.png

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
    }

clipboard.png

Guess you like

Origin www.cnblogs.com/baimeishaoxia/p/11807367.html