canvas 学习 3---画坐标系


本文摘自PHP中文网,作者坏嘻嘻,侵删。

无论您是新手,还是老手,本教程都值得一读。

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

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>网格</title>

    <style>

        #c1{

            border:1px solid black;

        }

 

    </style>

 

    <script>

            window.onload = function(){

                var oCanvas = document.getElementById('c1')

                var gd = oCanvas.getContext('2d')              

                // 你要画一个表格:

                // 你得思考,每个格子多大!

                var space = 20

                // 1, 得到 画布的宽和高

                var cWidth = gd.canvas.width;

                var cHeight = gd.canvas.height;

                // 当你记不住api 的时候,就打印出来看看!

                var lines = Math.floor(cHeight/space)

                var cols = Math.floor(cWidth/space)

                for(let i = 0;i<lines;i++){

                    gd.beginPath()

                    gd.moveTo(0,space*i-0.5)

                    gd.lineTo(cWidth,space*i-0.5)

                    gd.strokeStyle='#aaa'

                    gd.stroke();

                }

                // 画第二个竖着的格子!

             

                for(let j = 0; j<cols;j++){

                    gd.beginPath();

                    gd.moveTo(space*j-0.5,0)

                    gd.lineTo(space*j-0.5,cHeight)

                    gd.strokeStyle="#aaa"

                    gd.stroke()

                }

 

 

 

                // 下面是画那个坐标!

                 

                // 1, everPadding(坐标离 网格边界的上下左右的距离!)

                var everPadding = 40

                // 起点(坐标原点)

                var x0 = everPadding;

                var yo = cHeight -everPadding

                // x 轴,终点:

                var x1 = cWidth-everPadding;               

                // 竖着方向:

                 

                // 画着再说:

                gd.beginPath();

                gd.moveTo(x0,yo)

                gd.lineTo(x1,yo)

                gd.lineTo(x1-space,yo-space)

                gd.lineTo(x1-space,yo+space)

                gd.lineTo(x1,yo)

                gd.strokeStyle="red"

                gd.fillStyle="red"

                gd.stroke()

                gd.fill()

 

                gd.beginPath()

                gd.moveTo(x0,yo)

                gd.lineTo(x0,everPadding)

                gd.lineTo(x0-space,everPadding+space)

                gd.lineTo(x0+space,everPadding+space)

                gd.lineTo(x0,everPadding)

                gd.strokeStyle="red"

                gd.fillStyle="red"

                gd.stroke()

                gd.fill()

 

 

            }              

    </script>

 

</head>

<body>

     

     

        <canvas id="c1" width="500"  height="500"></canvas>

 

 

</body>

</html>


显示:

20180914084834590.png

相关推荐:

HTML5 Canvas入门学习教程_html5教程技巧

HTML5 Canvas之测试浏览器是否支持Canvas的方法_html5教程技巧

以上就是canvas 学习 3---画坐标系的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

html center标签怎么用

html怎么加链接

关于html页面跳转传递参数问题解答

html怎么引入js文件?

xhtml与html之间有什么区别?xhtml与html之间的异同分析

如何从javascript到typescript?

怎么删除html的注释

你必须知道的10个chrome开发工具和技巧

html5 简介

html table的直列化格式是什么

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




打赏

取消

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

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

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

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

评论

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