HTML5 Canvas的基本用法介绍


本文摘自PHP中文网,作者不言,侵删。

本篇文章给大家带来的内容是关于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 对象通过定时器来时时更新。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

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)

静止的图像如下图。

1056609250-5bf676267a57f_articlex.png

刮刮卡效果

使用 canvas 的图形合成的属性可以实现图片合成的效果。具体应用于刮刮卡。
globalCompositeOperation属性设置或返回如何将一个源(新的)图像绘制到目标(已有)的图像上
源图像 = 您打算放置到画布上的绘图
目标图像 = 您已经放置在画布上的绘图

3627601659-5bf676cdb872b_articlex.png

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

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

    }

964013326-5bf6bf6218884_articlex.png

以上就是HTML5 Canvas的基本用法介绍的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

js和Html的区别是什么

Html中head头结构应该如何使用

Html的编辑器使用

Html常用标签及属性

form标签的action属性怎么用?form标签action属性的用法介绍(附实例)

Html文件怎么弄注释

Html怎么注释单行

css怎么连接到Html

Html网页的主体标签是什么?

Html中url什么意思?怎么用?

更多相关阅读请进入《Html》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...