• canvas基础1


    直线

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. </head>
    8. <body>
    9. <canvas id="canvas" style="border:1px solid #ccc;margin:50px;"></canvas>
    10. <script>
    11. const canvas = document.getElementById('canvas')
    12. canvas.width = 1024
    13. canvas.height = 768
    14. const context = canvas.getContext('2d')
    15. // 绘制直线
    16. context.moveTo(100, 90) // 笔尖移动到 x:100 y:90 的位置
    17. context.lineTo(700, 600) // 画一条直线到 x: 700 y: 600 的位置
    18. context.lineWidth = 5 // 设置直线宽度为 5
    19. context.strokeStyle = '#005588' // 设置直线颜色
    20. // 以上只是设置直线状态,并未开始画
    21. context.stroke() // 将之前描述的线条状态画出来 stroke用于绘制线条
    22. </script>
    23. </body>
    24. </html>

    图示:

    线条的属性

    lineCap

    butt: 默认  

    round

    square

    设置线条 2端 的形态,2条线条相交处设置无效

    1. context.lineWidth = 50
    2. context.strokeStyle = '#058'
    3. context.beginPath()
    4. context.moveTo(100, 200)
    5. context.lineTo(700, 200)
    6. context.lineCap = 'butt'
    7. context.stroke()
    8. context.beginPath()
    9. context.moveTo(100, 400)
    10. context.lineTo(700, 400)
    11. context.lineCap = 'round'
    12. context.stroke()
    13. context.beginPath()
    14. context.moveTo(100, 600)
    15. context.lineTo(700, 600)
    16. context.lineCap = 'square'
    17. context.stroke()
    18. // 基线,用于对比lineCap的三种属性
    19. context.lineWidth = 1
    20. context.strokeStyle = '#27a'
    21. context.beginPath()
    22. context.moveTo(100, 100)
    23. context.lineTo(100, 700)
    24. context.moveTo(700, 100)
    25. context.lineTo(700, 700)
    26. context.stroke()

    图示:

    lineJoin

    miter:默认  尖角形状

    bevel:连接斜面

    round:圆角

    设置2条线条相交时的形态

    miter:

    1. const canvas = document.getElementById('canvas')
    2. canvas.width = 800
    3. canvas.height = 800
    4. const context = canvas.getContext('2d')
    5. const posX = 400
    6. const posY = 0
    7. const bigRadius = 300
    8. const smallRadius = 150
    9. context.lineWidth = 10
    10. context.lineJoin = 'miter'
    11. drawStar(context, smallRadius, bigRadius, posX, posY, 0)
    12. function drawStar(cxt, r, R, x, y, rot=0) {
    13. cxt.beginPath()
    14. for (let i = 0; i < 5; i++) {
    15. cxt.lineTo(
    16. Math.cos((18 + i * 72 - rot) / 180 * Math.PI) * R + x,
    17. -Math.sin((18 + i * 72 - rot) / 180 * Math.PI) * R + x
    18. )
    19. cxt.lineTo(
    20. Math.cos((54 + i * 72 - rot) / 180 * Math.PI) * r + x,
    21. -Math.sin((54 + i * 72 - rot) / 180 * Math.PI) * r + x
    22. )
    23. }
    24. cxt.closePath()
    25. context.stroke()
    26. }

    图示:

    bevel:

    context.lineJoin = 'bevel'

    图示:

    round:

    context.lineJoin = 'round'

    图示:

    miter

    当线条属性 lineJoin 为 miter  时有效,

    内角和外角之间的距离

    默认为 10

    矩形

    rect(x, y, width, height)

    x: 矩形左上角的 x 坐标

    y: 矩形左上角的 y 坐标

    width: 矩形的宽度

    height: 矩形的高度

    fillRect(x, y, width, height)  绘制矩形并填充颜色

    strokeRect(x, y, width, height)  绘制矩形并描边

    后面绘制的图形会覆盖前面的图形

    1. context.lineWidth = 5
    2. context.strokeStyle = '#058'
    3. context.fillStyle = 'red'
    4. context.beginPath()
    5. context.rect(100, 100, 400, 300)
    6. context.closePath()
    7. context.fill()
    8. context.stroke()
    9. context.fillStyle = 'green'
    10. context.beginPath()
    11. context.fillRect(300, 300, 400, 300)
    12. context.strokeRect(300, 300, 400, 300)
    13. context.closePath()

    图示:

    多边形

    多边形就是多个线条连接起来

    1. // 绘制多边形
    2. context.moveTo(100, 90) // 笔尖移动到 x:100 y:90 的位置
    3. context.lineTo(700, 600) // 画一条直线到 x: 700 y: 600 的位置
    4. context.lineTo(100, 600)
    5. context.lineTo(100, 90)
    6. context.fillStyle = 'rgb(2, 100, 30)' // 设置形状内的填充色
    7. context.fill() // 填充颜色
    8. context.lineWidth = 5 // 设置直线宽度为 5
    9. context.strokeStyle = '#005588' // 设置直线颜色
    10. // 以上只是设置直线状态,并未开始画
    11. context.stroke() // 将之前描述的线条状态画出来 stroke用于绘制线条

    图示:

    多条线段

    灵活使用 moveTo 函数,调整笔尖位置开始绘制

    1. context.moveTo(100, 200)
    2. context.lineTo(300, 400)
    3. context.lineTo(100, 600)
    4. context.moveTo(300, 200)
    5. context.lineTo(500, 400)
    6. context.lineTo(300, 600)
    7. context.moveTo(500, 200)
    8. context.lineTo(700, 400)
    9. context.lineTo(500, 600)
    10. context.lineWidth = 5
    11. context.strokeStyle = '#058'
    12. context.stroke()

    图示:

    多个图形

    stroke函数 会将所有内容重新绘制,因此后面设置的线段的宽度、颜色等会覆盖之前的设定

    需要用 beginPath 声明重新绘制一个线段, 这样 stroke函数 将从 beiginPath 开始绘制

    closePath 会将 未闭合 的图形 用直线 将图形闭合

    1. // 绘制多个图形
    2. context.beginPath()
    3. context.moveTo(100, 90) // 笔尖移动到 x:100 y:90 的位置
    4. context.lineTo(700, 600) // 画一条直线到 x: 700 y: 600 的位置
    5. context.lineTo(100, 600)
    6. context.lineTo(100, 90)
    7. context.closePath()
    8. context.fillStyle = 'rgb(2, 100, 30)' // 设置形状内的填充色
    9. context.fill() // 填充颜色
    10. context.lineWidth = 5 // 设置直线宽度为 5
    11. context.strokeStyle = '#005588' // 设置直线颜色
    12. // 以上只是设置直线状态,并未开始画
    13. context.stroke() // 将之前描述的线条状态画出来 stroke用于绘制线条
    14. context.beginPath()
    15. context.moveTo(200, 100)
    16. context.lineTo(800, 600)
    17. context.closePath()
    18. context.strokeStyle = 'black'
    19. context.stroke()

    图示:

    弧线

    arc

    context.arc(centerx, centery, radius, startingAngle, endingAngle, anticlockwise = false)

    centerx: x坐标
    centery: y坐标
    radius: 半径
    startingAngle: 从哪个弧度值开始
    endingAngle: 结束于哪个弧度值
    anticlockwise: 是否逆时针绘制,默认为 false,也就是顺时针绘制

    弧度值

    无论是顺时针绘制还是逆时针绘制,弧度制是不变的

    顺时针:

    1. // 绘制弧线
    2. /*
    3. context.arc(centerx, centery, radius, startingAngle, endingAngle, anticlockwise = false)
    4. centerx: x坐标
    5. centery: y坐标
    6. radius: 半径
    7. startingAngle: 从哪个弧度值开始
    8. endingAngle: 结束于哪个弧度值
    9. anticlockwise: 是否逆时针绘制,默认为 false,也就是顺时针绘制
    10. */
    11. context.lineWidth = 5
    12. context.strokeStyle = '#005588'
    13. context.arc(300, 300, 200, 0, 1.5*Math.PI)
    14. context.stroke()

    图示:

    逆时针:

    1. context.lineWidth = 5
    2. context.strokeStyle = '#005588'
    3. context.arc(300, 300, 200, 0, 1.5*Math.PI, true)
    4. context.stroke()

    图示:

    arcTo

    将上文 moveTo 或 lineTo 的坐标作为 x0,y0

    代码:

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. </head>
    8. <body>
    9. <canvas id="canvas" style="border:1px solid #ccc;display:block;margin:50px auto;"></canvas>
    10. <script>
    11. const canvas = document.getElementById('canvas')
    12. canvas.width = 800
    13. canvas.height = 800
    14. const context = canvas.getContext('2d')
    15. arcToTest(context, 150, 100, 650, 100, 650, 600, 300)
    16. function arcToTest(cxt, x0, y0, x1, y1, x2, y2, R) {
    17. context.beginPath()
    18. context.moveTo(x0, y0)
    19. context.arcTo(x1, y1, x2, y2, R)
    20. context.lineWidth = 6
    21. context.strokeStyle = 'red'
    22. context.stroke()
    23. // baseline
    24. context.beginPath()
    25. context.moveTo(x0, y0)
    26. context.lineTo(x1, y1)
    27. context.lineTo(x2, y2)
    28. context.lineWidth = 2
    29. context.strokeStyle = 'gray'
    30. context.stroke()
    31. }
    32. </script>
    33. </body>
    34. </html>

    图示:

    贝塞尔二次曲线

    参考网站

    Canvas Quadratic Curve Example

    贝塞尔三次曲线

    Canvas Bézier Curve Example

    椭圆

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. </head>
    8. <body>
    9. <canvas id="canvas" style="border: 1px solid #aaa;display:block;margin: 20px auto;"></canvas>
    10. <script>
    11. const canvas = document.getElementById('canvas')
    12. canvas.width = 800
    13. canvas.height = 800
    14. const context = canvas.getContext('2d')
    15. context.beginPath()
    16. context.ellipse(400, 400, 300, 200, 0, 0, Math.PI*2)
    17. context.stroke()
    18. </script>
    19. </body>
    20. </html>

    图示:

    线性渐变

    1. const canvas = document.getElementById('canvas')
    2. canvas.width = 800
    3. canvas.height = 800
    4. const context = canvas.getContext('2d')
    5. const linearGrad = context.createLinearGradient(0, 0, 800, 800)
    6. linearGrad.addColorStop(0.0, '#fff')
    7. linearGrad.addColorStop(0.25, 'yellow')
    8. linearGrad.addColorStop(0.5, 'green')
    9. linearGrad.addColorStop(0.75, 'blue')
    10. linearGrad.addColorStop(1.0, '#000')
    11. context.fillStyle = linearGrad
    12. context.fillRect(0, 0, 800, 800)

    图示:

    水平渐变

    const linearGrad = context.createLinearGradient(0, 0, 800, 0)

    垂直渐变

    const linearGrad = context.createLinearGradient(0, 0, 0, 800)

    径向渐变

    1. const canvas = document.getElementById('canvas')
    2. canvas.width = 800
    3. canvas.height = 800
    4. const context = canvas.getContext('2d')
    5. const linearGrad = context.createRadialGradient(400, 400, 100, 400, 400, 500)
    6. linearGrad.addColorStop(0.0, '#fff')
    7. linearGrad.addColorStop(0.25, 'yellow')
    8. linearGrad.addColorStop(0.5, 'green')
    9. linearGrad.addColorStop(0.75, 'blue')
    10. linearGrad.addColorStop(1.0, '#000')
    11. context.fillStyle = linearGrad
    12. context.fillRect(0, 0, 800, 800)

    图示:

    图片填充

    createPattern

    1. const canvas = document.getElementById('canvas')
    2. canvas.width = 800
    3. canvas.height = 800
    4. const context = canvas.getContext('2d')
    5. const backgroundImage = new Image()
    6. backgroundImage.src = 'a.jpg'
    7. backgroundImage.onload = function() {
    8. const pattern = context.createPattern(backgroundImage, 'repeat')
    9. context.fillStyle = pattern
    10. context.fillRect(0, 0, 800, 800)
    11. }

    图示:

    文字渲染

    文字渲染

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. </head>
    8. <body>
    9. <canvas id="canvas" style="border:1px solid #ccc;display:block;margin:50px auto;"></canvas>
    10. <script>
    11. const canvas = document.getElementById('canvas')
    12. canvas.width = 800
    13. canvas.height = 800
    14. const context = canvas.getContext('2d')
    15. context.font = 'bold 40px Arial'
    16. context.fillStyle = '#058'
    17. context.fillText('欢迎大家学习《Canvas绘图接口详解》!', 40, 100)
    18. </script>
    19. </body>
    20. </html>

    图示:

    文字描边

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. </head>
    8. <body>
    9. <canvas id="canvas" style="border:1px solid #ccc;display:block;margin:50px auto;"></canvas>
    10. <script>
    11. const canvas = document.getElementById('canvas')
    12. canvas.width = 800
    13. canvas.height = 800
    14. const context = canvas.getContext('2d')
    15. context.font = 'bold 40px Arial'
    16. context.fillStyle = '#058'
    17. context.fillText('欢迎大家学习《Canvas绘图接口详解》!', 40, 100)
    18. context.lineWidth = 1
    19. context.strokeStyle = '#058'
    20. context.strokeText('欢迎大家学习《Canvas绘图接口详解》!', 40, 200)
    21. </script>
    22. </body>
    23. </html>

    图示:

    设置文字的宽度

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. </head>
    8. <body>
    9. <canvas id="canvas" style="border:1px solid #ccc;display:block;margin:50px auto;"></canvas>
    10. <script>
    11. const canvas = document.getElementById('canvas')
    12. canvas.width = 800
    13. canvas.height = 800
    14. const context = canvas.getContext('2d')
    15. context.font = 'bold 40px Arial'
    16. context.fillStyle = '#058'
    17. context.fillText('欢迎大家学习《Canvas绘图接口详解》!', 40, 100)
    18. context.lineWidth = 1
    19. context.strokeStyle = '#058'
    20. context.strokeText('欢迎大家学习《Canvas绘图接口详解》!', 40, 200)
    21. context.fillText('欢迎大家学习《Canvas绘图接口详解》!', 40, 300, 400)
    22. context.strokeText('欢迎大家学习《Canvas绘图接口详解》!', 40, 400, 400)
    23. </script>
    24. </body>
    25. </html>

    图示:

    文字渐变样式

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. </head>
    8. <body>
    9. <canvas id="canvas" style="border:1px solid #ccc;display:block;margin:50px auto;"></canvas>
    10. <script>
    11. const canvas = document.getElementById('canvas')
    12. canvas.width = 800
    13. canvas.height = 800
    14. const context = canvas.getContext('2d')
    15. context.font = 'bold 40px Arial'
    16. context.fillStyle = '#058'
    17. context.fillText('欢迎大家学习《Canvas绘图接口详解》!', 40, 100)
    18. context.lineWidth = 1
    19. context.strokeStyle = '#058'
    20. context.strokeText('欢迎大家学习《Canvas绘图接口详解》!', 40, 200)
    21. context.fillText('欢迎大家学习《Canvas绘图接口详解》!', 40, 300, 400)
    22. context.strokeText('欢迎大家学习《Canvas绘图接口详解》!', 40, 400, 400)
    23. const linearGrad = context.createLinearGradient(0, 0, 800, 0)
    24. linearGrad.addColorStop(0.0, 'red')
    25. linearGrad.addColorStop(0.25, 'orange')
    26. linearGrad.addColorStop(0.5, 'yellow')
    27. linearGrad.addColorStop(0.75, 'green')
    28. linearGrad.addColorStop(1.0, 'purple')
    29. context.fillStyle = linearGrad
    30. context.fillText('欢迎大家学习《Canvas绘图接口详解》!', 40, 500)
    31. </script>
    32. </body>
    33. </html>

    图示:

    设置文字背景

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. </head>
    8. <body>
    9. <canvas id="canvas" style="border:1px solid #ccc;display:block;margin:50px auto;"></canvas>
    10. <script>
    11. const canvas = document.getElementById('canvas')
    12. canvas.width = 800
    13. canvas.height = 800
    14. const context = canvas.getContext('2d')
    15. context.font = 'bold 40px Arial'
    16. context.fillStyle = '#058'
    17. context.fillText('欢迎大家学习《Canvas绘图接口详解》!', 40, 100)
    18. context.lineWidth = 1
    19. context.strokeStyle = '#058'
    20. context.strokeText('欢迎大家学习《Canvas绘图接口详解》!', 40, 200)
    21. context.fillText('欢迎大家学习《Canvas绘图接口详解》!', 40, 300, 400)
    22. context.strokeText('欢迎大家学习《Canvas绘图接口详解》!', 40, 400, 400)
    23. const linearGrad = context.createLinearGradient(0, 0, 800, 0)
    24. linearGrad.addColorStop(0.0, 'red')
    25. linearGrad.addColorStop(0.25, 'orange')
    26. linearGrad.addColorStop(0.5, 'yellow')
    27. linearGrad.addColorStop(0.75, 'green')
    28. linearGrad.addColorStop(1.0, 'purple')
    29. context.fillStyle = linearGrad
    30. context.fillText('欢迎大家学习《Canvas绘图接口详解》!', 40, 500)
    31. const backgroundImage = new Image()
    32. backgroundImage.src = 'a.jpg'
    33. backgroundImage.onload = function () {
    34. const pattern = context.createPattern(backgroundImage, 'repeat')
    35. context.fillStyle = pattern
    36. context.font = 'bold 100px Arial'
    37. context.fillText('Canvas', 40, 650)
    38. context.strokeText('Canvas', 40, 650)
    39. }
    40. </script>
    41. </body>
    42. </html>

    图示:

    设置字体

    font-style

    normal (默认)

    italic     (斜体字)

    oblique (倾斜字体)

    font-variant

    normal (默认)

    small-caps (小型大写字母)

    例:context.font = 'small-caps bold 40px sans-serif'

    font-weight

    lighter

    normal (默认)

    bold

    bolder

    font-size

    20px (默认)

    2em

    150%

    font-family

    设置多种字体备选

    支持 @font-face

    Web安全字体

    文本对齐

    水平对齐

    context.textAlign =  left

                                    center

                                    right

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. </head>
    8. <body>
    9. <canvas id="canvas" style="border:1px solid #ccc;display:block;margin:50px auto;"></canvas>
    10. <script>
    11. const canvas = document.getElementById('canvas')
    12. canvas.width = 800
    13. canvas.height = 800
    14. const context = canvas.getContext('2d')
    15. context.font = 'bold 40px Arial'
    16. context.fillStyle = '#058'
    17. context.textAlign = 'left'
    18. context.fillText('textAlign = left', 400, 100)
    19. context.textAlign = 'center'
    20. context.fillText('textAlign = center', 400, 200)
    21. context.textAlign = 'right'
    22. context.fillText('textAlign = right', 400, 300)
    23. // baseline
    24. context.strokeStyle = '#888'
    25. context.moveTo(400, 0)
    26. context.lineTo(400, 800)
    27. context.stroke()
    28. </script>
    29. </body>
    30. </html>

    图示:

    垂直对齐

    context.textBaseline =  top

                                         middle

                                         bottom

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. </head>
    8. <body>
    9. <canvas id="canvas" style="border:1px solid #ccc;display:block;margin:50px auto;"></canvas>
    10. <script>
    11. const canvas = document.getElementById('canvas')
    12. canvas.width = 800
    13. canvas.height = 800
    14. const context = canvas.getContext('2d')
    15. context.font = 'bold 40px Arial'
    16. context.fillStyle = '#058'
    17. context.textBaseline = 'top'
    18. context.fillText('textBaseline = top', 40, 100)
    19. drawBaseline(context, 100)
    20. context.textBaseline = 'middle'
    21. context.fillText('textBaseline = middle', 40, 200)
    22. drawBaseline(context, 200)
    23. context.textBaseline = 'bottom'
    24. context.fillText('textBaseline = bottom', 40, 300)
    25. drawBaseline(context, 300)
    26. function drawBaseline(cxt, h) {
    27. const width = cxt.canvas.width
    28. cxt.save()
    29. cxt.strokeStyle = '#888'
    30. cxt.lineWidth = 2
    31. cxt.moveTo(0, h)
    32. cxt.lineTo(width, h)
    33. cxt.stroke()
    34. cxt.restore()
    35. }
    36. </script>
    37. </body>
    38. </html>

    图示:

    文本的度量

    context.measureText(string).width  获取文本的宽度

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. </head>
    8. <body>
    9. <canvas id="canvas" style="border:1px solid #ccc;display:block;margin:50px auto;"></canvas>
    10. <script>
    11. const canvas = document.getElementById('canvas')
    12. canvas.width = 800
    13. canvas.height = 800
    14. const context = canvas.getContext('2d')
    15. context.font = 'bold 40px Arial'
    16. context.fillStyle = '#058'
    17. context.fillText('欢迎大家学习《Canvas绘图接口详解》!', 40, 100)
    18. const textWidth = context.measureText('欢迎大家学习《Canvas绘图接口详解》!').width
    19. context.fillText('以上字符串的宽度为' + textWidth + 'px', 40, 200)
    20. </script>
    21. </body>
    22. </html>

    图示:

    阴影

    context.shadowColor   阴影的颜色

    context.shadowOffsetX   阴影的位移值

    context.shadowOffsetY   阴影的位移值

    context.shadowBlur   阴影的模糊程度  如果为0不模糊  值越大,越模糊

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. </head>
    8. <body>
    9. <canvas id="canvas" style="border:1px solid #ccc;display:block;margin:50px auto;"></canvas>
    10. <script>
    11. const canvas = document.getElementById('canvas')
    12. canvas.width = 800
    13. canvas.height = 800
    14. const context = canvas.getContext('2d')
    15. context.fillStyle = '#058'
    16. context.shadowColor = 'gray'
    17. context.shadowOffsetX = 20
    18. context.shadowOffsetY = 20
    19. context.shadowBlur = 5
    20. context.fillRect(200, 200, 400, 400)
    21. </script>
    22. </body>
    23. </html>

    图示:

    global

    globalAlpha

    让全局 绘制的图像 具有透明度

    默认值: 1

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. </head>
    8. <body>
    9. <canvas id="canvas" style="border:1px solid #ccc;display:block;margin:50px auto;"></canvas>
    10. <script>
    11. const canvas = document.getElementById('canvas')
    12. canvas.width = 1200
    13. canvas.height = 800
    14. const context = canvas.getContext('2d')
    15. context.globalAlpha = 0.7
    16. for(let i = 0; i < 100; i++) {
    17. const R = Math.floor(Math.random() * 255)
    18. const G = Math.floor(Math.random() * 255)
    19. const B = Math.floor(Math.random() * 255)
    20. context.fillStyle = `rgb(${R}, ${G}, ${B})`
    21. context.beginPath()
    22. context.arc(Math.random()*canvas.width, Math.random()*canvas.height, Math.random()*100, 0, Math.PI*2)
    23. context.fill()
    24. }
    25. </script>
    26. </body>
    27. </html>

    图示:

    globalCompositeOperation

    绘制的图像所产生的效果

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. <style>
    8. #buttons {
    9. width: 1200px;
    10. margin: 10px auto;
    11. display: flex;
    12. }
    13. #buttons a {
    14. font-size: 18px;
    15. margin-right: 14px;
    16. }
    17. </style>
    18. </head>
    19. <body>
    20. <canvas id="canvas" style="border:1px solid #ccc;display:block;margin:10px auto;"></canvas>
    21. <div id="buttons">
    22. <a href="#">source-over</a>
    23. <a href="#">source-atop</a>
    24. <a href="#">source-in</a>
    25. <a href="#">source-out</a>
    26. <a href="#">destination-over</a>
    27. <a href="#">destination-atop</a>
    28. <a href="#">destination-in</a>
    29. <a href="#">destination-out</a>
    30. <a href="#">lighter</a>
    31. <a href="#">copy</a>
    32. <a href="#">xor</a>
    33. </div>
    34. <script>
    35. const canvas = document.getElementById('canvas')
    36. canvas.width = 1200
    37. canvas.height = 800
    38. const context = canvas.getContext('2d')
    39. draw(context, 'source-over')
    40. const buttons = document.getElementsByTagName('a')
    41. for (let i = 0; i < buttons.length; i++) {
    42. buttons[i].onclick = function() {
    43. draw(context, this.text)
    44. return false
    45. }
    46. }
    47. function draw(ctx, compositeStyle) {
    48. ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height)
    49. // draw title
    50. ctx.font = 'bold 40px Arial'
    51. ctx.textAlign = 'center'
    52. ctx.textBaseline = 'middle'
    53. ctx.fillStyle = '#058'
    54. ctx.fillText('globalCompositeOperation = ' + compositeStyle, ctx.canvas.width/2, 60)
    55. // draw a rect
    56. ctx.fillStyle = 'blue'
    57. ctx.fillRect(300, 150, 500, 500)
    58. // draw a triangle
    59. ctx.globalCompositeOperation = compositeStyle
    60. ctx.fillStyle = 'red'
    61. ctx.beginPath()
    62. ctx.moveTo(700, 250)
    63. ctx.lineTo(1000, 750)
    64. ctx.lineTo(400, 750)
    65. ctx.closePath()
    66. ctx.fill()
    67. }
    68. </script>
    69. </body>
    70. </html>

    source-over  后绘制的图像压盖在前面绘制的图像上  (默认)

    source-atop

    source-in

    source-out

    destination-over  前面绘制的图像压盖在后面绘制的图像上

    destination-atop

    destination-in

    destination-out

    lighter

    copy (只绘制最后一个图形)

    xor   图形之间进行 异或 操作

    剪辑区域

    context.clip()

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. </head>
    8. <body>
    9. <canvas id="canvas" style="border:1px solid #ccc;display:block;margin:10px auto;"></canvas>
    10. </div>
    11. <script>
    12. const canvas = document.getElementById('canvas')
    13. canvas.width = 800
    14. canvas.height = 800
    15. const context = canvas.getContext('2d')
    16. context.beginPath()
    17. context.fillStyle = 'black'
    18. context.fillRect(0, 0, canvas.width, canvas.height)
    19. context.beginPath()
    20. context.arc(400, 400, 150, 0, Math.PI*2)
    21. context.fillStyle = '#fff'
    22. context.fill()
    23. context.clip()
    24. context.font = 'bold 150px Arial'
    25. context.textAlign = 'center'
    26. context.textBaseline = 'middle'
    27. context.fillStyle = '#058'
    28. context.fillText('CANVAS', canvas.width / 2, canvas.height / 2)
    29. </script>
    30. </body>
    31. </html>

    图示:

    isPointInPath

    context.isPointInPath(x, y)

    确定坐标是否在某个图形内

    扩展 canvas 的 context

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    6. <title>Document</title>
    7. </head>
    8. <body>
    9. <canvas id="canvas" style="border: 1px solid #aaa;display:block;margin: 20px auto;"></canvas>
    10. <script>
    11. CanvasRenderingContext2D.prototype.fillStar = function(r, R, x, y, rot) {
    12. this.beginPath()
    13. for (let i = 0; i < 5; i++) {
    14. this.lineTo(
    15. Math.cos((18 + i * 72 - rot) / 180 * Math.PI) * R + x,
    16. -Math.sin((18 + i * 72 - rot) / 180 * Math.PI) * R + y
    17. )
    18. this.lineTo(
    19. Math.cos((54 + i * 72 - rot) / 180 * Math.PI) * r + x,
    20. -Math.sin((54 + i * 72 - rot) / 180 * Math.PI) * r + y
    21. )
    22. }
    23. this.closePath()
    24. this.fill()
    25. }
    26. const canvas = document.getElementById('canvas')
    27. canvas.width = 800
    28. canvas.height = 800
    29. const context = canvas.getContext('2d')
    30. context.fillStyle = '#058'
    31. context.fillStar(150, 300, 400, 400, 0)
    32. </script>
    33. </body>
    34. </html>

    图示:

    1

  • 相关阅读:
    【广州华锐互动】三维全景3D消防科普展馆
    xubuntu16.04系统中隐藏网络连接的弹窗提示
    k3s 搭建高可用rancher
    金融日 | 看金融机构数据安全建设典型案例
    【@property的参数copy Objective-C语言】
    【递归】树形结构、list转map
    基于SSM的酒店客房管理系统设计与实现
    CPU流水线与指令乱序执行
    Facebook内容的类型
    设计模式-迭代器模式(Iterator)
  • 原文地址:https://blog.csdn.net/m0_38066007/article/details/133745657