码迷,mamicode.com
首页 > 其他好文 > 详细

canvas

时间:2019-11-17 18:00:42      阅读:72      评论:0      收藏:0      [点我收藏+]

标签:round   缩放   重复   lte   容器   pos   空白   init   head   

canvas描述

    HTML5 <canvas> 标签用于绘制图像(通过脚本,通常是 JavaScript)。 不过,<canvas> 元素本身并没有绘制能力(它仅仅是图形的容器) - 您必须使用脚本来完成实际的绘图任务。 getContext() 方法可返回一个对象,该对象提供了用于在画布上绘图的方法和属性。参考

canvas属性

  • 颜色、样式和阴影
方法描述
createLinearGradient() 创建线性渐变(用在画布内容上)。
createPattern() 在指定的方向上重复指定的元素。
createRadialGradient() 创建放射状/环形的渐变(用在画布内容上)。
addColorStop() 规定渐变对象中的颜色和停止位置。
  • 线条样式
属性描述
lineCap 设置或返回线条的结束端点样式。
lineJoin 设置或返回两条线相交时,所创建的拐角类型。
lineWidth 设置或返回当前的线条宽度。
miterLimit 设置或返回最大斜接长度。
  • 矩形
方法描述
rect() 创建矩形。
fillRect() 绘制"被填充"的矩形。
strokeRect() 绘制矩形(无填充)。
clearRect() 在给定的矩形内清除指定的像素。
  • 路径
方法描述
fill() 填充当前绘图(路径)。
stroke() 绘制已定义的路径。
beginPath() 起始一条路径,或重置当前路径。
moveTo() 把路径移动到画布中的指定点,不创建线条。
closePath() 创建从当前点回到起始点的路径。
lineTo() 添加一个新点,然后在画布中创建从该点到最后指定点的线条。
clip() 从原始画布剪切任意形状和尺寸的区域。
quadraticCurveTo() 创建二次贝塞尔曲线。
bezierCurveTo() 创建三次贝塞尔曲线。
arc() 创建弧/曲线(用于创建圆形或部分圆)。
arcTo() 创建两切线之间的弧/曲线。
isPointInPath() 如果指定的点位于当前路径中,则返回 true,否则返回 false。
  • 转换
方法描述
scale() 缩放当前绘图至更大或更小。
rotate() 旋转当前绘图。
translate() 重新映射画布上的 (0,0) 位置。
transform() 替换绘图的当前转换矩阵。
setTransform() 将当前转换重置为单位矩阵。然后运行 transform()。
  • 文本
属性描述
font 设置或返回文本内容的当前字体属性。
textAlign 设置或返回文本内容的当前对齐方式。
textBaseline 设置或返回在绘制文本时使用的当前文本基线。
方法描述
fillText() 在画布上绘制"被填充的"文本。
strokeText() 在画布上绘制文本(无填充)。
measureText() 返回包含指定文本宽度的对象。
  • 像素设置
属性描述
width 返回 ImageData 对象的宽度。
height 返回 ImageData 对象的高度。
data 返回一个对象,其包含指定的 ImageData 对象的图像数据。
方法描述
createImageData() 创建新的、空白的 ImageData 对象。
getImageData() 返回 ImageData 对象,该对象为画布上指定的矩形复制像素数据。
putImageData() 把图像数据(从指定的 ImageData 对象)放回画布上。
  • 合成
属性描述
globalAlpha 设置或返回绘图的当前 alpha 或透明值。
globalCompositeOperation 设置或返回新图像如何绘制到已有的图像上。

图像绘制

  • HTML canvas drawImage() 方法

实例1

技术图片
<p>要使用的图片:</p>
<img id="scream" src="https://www.runoob.com/wp-content/uploads/2013/11/img_the_scream.jpg" alt="">
<p>画布:</p>
<canvas id="myCanvas" width="250" height="300" style="border:1px solid #d3d3d3;">
         您的浏览器不支持 HTML5 canvas 标签。
</canvas>
<script>
      var c = document.getElementById("myCanvas");
      var ctx = c.getContext("2d");
      var img = document.getElementById("scream");
      img.onload = function () {
           ctx.drawImage(img, 10, 10);
      }
</script>
View Code

 

 

 

 

参数值
参数描述
img 规定要使用的图像、画布或视频。
sx 可选。开始剪切的 x 坐标位置。
sy 可选。开始剪切的 y 坐标位置。
swidth 可选。被剪切图像的宽度。
sheight 可选。被剪切图像的高度。
x 在画布上放置图像的 x 坐标位置。
y 在画布上放置图像的 y 坐标位置。
width 可选。要使用的图像的宽度(伸展或缩小图像)。
height 可选。要使用的图像的高度(伸展或缩小图像)。

实例2

 

技术图片
<p>要使用的视频:</p>
<video id="video1" controls width="270" autoplay>
<source src="https://www.runoob.com/wp-content/uploads/2013/11/mov_bbb.mp4" type=‘video/mp4‘>
<source src="https://www.runoob.com/wp-content/uploads/2013/11/mov_bbb.mp4" type=‘video/ogg‘>
<source src="https://www.runoob.com/wp-content/uploads/2013/11/mov_bbb.mp4" type=‘video/webm‘>
</video>

<p>画布 (代码在每20毫秒绘制当前的视频帧):</p>
<canvas id="myCanvas" width="270" height="135" style="border:1px solid #d3d3d3;">
您的浏览器不支持 HTML5 canvas 标签。
</canvas>
<script>
      var v = document.getElementById("video1");
      var c = document.getElementById("myCanvas");
      ctx = c.getContext(2d);
      v.addEventListener(play, function () {
             var i = window.setInterval(function () {
              ctx.drawImage(v, 5, 5, 260, 125)
                  }, 20);
               }, false);
              v.addEventListener(pause, function () {
                   window.clearInterval(i);
               }, false);
               v.addEventListener(ended, function () {
                    clearInterval(i);
                }, false);
</script>
View Code

 

 

CSS3的动画属性
属性描述
@keyframes 规定动画。
animation 所有动画属性的简写属性,除了 animation-play-state 属性。
animation-name 规定 @keyframes 动画的名称。
animation-duration 规定动画完成一个周期所花费的秒或毫秒。默认是 0。
animation-timing-function 规定动画的速度曲线。默认是 "ease"。
animation-fill-mode 规定当动画不播放时(当动画完成时,或当动画有一个延迟未开始播放时),要应用到元素的样式。
animation-delay 规定动画何时开始。默认是 0。
animation-iteration-count 规定动画被播放的次数。默认是 1。
animation-direction 规定动画是否在下一周期逆向地播放。默认是 "normal"。
animation-play-state 规定动画是否正在运行或暂停。默认是 "running"。

实例3:绘制太阳系各大行星运行轨迹:

技术图片
<div class="solarsys">
                
                    <div class=‘sun‘></div>

                
                    <div class=‘mercuryOrbit‘></div>

                
                    <div class=‘mercury‘></div>

                
                    <div class=‘venusOrbit‘></div>

                
                    <div class=‘venus‘></div>

                
                    <div class=‘earthOrbit‘></div>

                
                    <div class=‘earth‘></div>

                
                    <div class=‘marsOrbit‘></div>

                
                    <div class=‘mars‘></div>

                
                    <div class=‘jupiterOrbit‘></div>

                
                    <div class=‘jupiter‘></div>

                
                    <div class=‘saturnOrbit‘></div>

                
                    <div class=‘saturn‘></div>

                
                    <div class=‘uranusOrbit‘></div>

                
                    <div class=‘uranus‘></div>

                
                    <div class=‘neptuneOrbit‘></div>

                
                    <div class=‘neptune‘></div>
                </div>
View Code

 

技术图片
//css代码
                <style>
                    .solarsys {
                        width: 800px;
                        height: 800px;;
                        position: relative;
                        margin: 0 auto;
                        background-color: #000000;
                        padding: 0;
                        transform: scale(1);
                    }

                    /*太阳*/
                    .sun {
                        left: 357px;
                        top: 357px;
                        height: 90px;
                        width: 90px;
                        background-color: rgb(248, 107, 35);
                        border-radius: 50%;
                        box-shadow: 5px 5px 10px rgb(248, 107, 35), -5px -5px 10px rgb(248, 107, 35), 5px -5px 10px rgb(248, 107, 35), -5px 5px 10px rgb(248, 107, 35);
                        position: absolute;
                        margin: 0;
                    }

                    /*水星*/
                    .mercury {
                        left: 337.5px;
                        top: 395px;
                        height: 10px;
                        width: 10px;
                        background-color: rgb(166, 138, 56);
                        border-radius: 50%;
                        position: absolute;
                        transform-origin: 62.5px 5px;
                        animation: rotate 1.5s infinite linear;
                    }

                    /*水星轨道*/
                    .mercuryOrbit {
                        left: 342.5px;
                        top: 342.5px;
                        height: 115px;
                        width: 115px;
                        background-color: transparent;
                        border-radius: 50%;
                        border-style: dashed;
                        border-color: gray;
                        position: absolute;
                        border-width: 1px;
                        margin: 0;
                        padding: 0;
                    }

                    /*金星*/
                    .venus {
                        left: 309px;
                        top: 389px;
                        height: 22px;
                        width: 22px;
                        background-color: rgb(246, 157, 97);
                        border-radius: 50%;
                        position: absolute;
                        transform-origin: 91px 11px;
                        animation: rotate 3.84s infinite linear;
                    }

                    /*金星轨道*/
                    .venusOrbit {
                        left: 320px;
                        top: 320px;
                        height: 160px;
                        width: 160px;
                        background-color: transparent;
                        border-radius: 50%;
                        border-style: dashed;
                        border-color: gray;
                        position: absolute;
                        border-width: 1px;
                        /*margin: 100px;*/
                        /*transform-origin: -75px -75px;*/
                        /*animation: rotate 4s infinite linear;*/
                        margin: 0;
                        padding: 0;
                    }

                    /*地球*/
                    .earth {
                        left: 266.5px;
                        top: 391px;
                        height: 18px;
                        width: 18px;
                        background-color: rgb(115, 114, 174);
                        border-radius: 50%;
                        position: absolute;
                        transform-origin: 134px 9px;
                        animation: rotate 6.25s infinite linear;
                    }

                    /*地球轨道*/
                    .earthOrbit {
                        left: 275px;
                        top: 275px;
                        height: 250px;
                        width: 250px;
                        background-color: transparent;
                        border-radius: 50%;
                        border-style: dashed;
                        border-color: gray;
                        position: absolute;
                        border-width: 1px;
                        /*margin: 100px;*/
                        /*transform-origin: -75px -75px;*/
                        /*animation: rotate 4s infinite linear;*/
                        margin: 0;
                        padding: 0;
                    }

                    /*火星*/
                    .mars {
                        left: 222.5px;
                        top: 392.5px;
                        height: 15px;
                        width: 15px;
                        background-color: rgb(140, 119, 63);
                        border-radius: 50%;
                        position: absolute;
                        transform-origin: 177.5px 7.5px;
                        animation: rotate 11.75s infinite linear;
                    }

                    /*火星轨道*/
                    .marsOrbit {
                        left: 230px;
                        top: 230px;
                        height: 340px;
                        width: 340px;
                        background-color: transparent;
                        border-radius: 50%;
                        border-style: dashed;
                        border-color: gray;
                        position: absolute;
                        border-width: 1px;
                        /*margin: 100px;*/
                        /*transform-origin: -75px -75px;*/
                        /*animation: rotate 4s infinite linear;*/
                        margin: 0;
                        padding: 0;
                    }

                    /*木星*/
                    .jupiter {
                        left: 134px;
                        top: 379px;
                        height: 42px;
                        width: 42px;
                        background-color: rgb(156, 164, 143);
                        border-radius: 50%;
                        position: absolute;
                        transform-origin: 266px 21px;
                        animation: rotate 74.04s infinite linear;
                    }

                    /*木星轨道*/
                    .jupiterOrbit {
                        left: 155px;
                        top: 155px;
                        height: 490px;
                        width: 490px;
                        background-color: transparent;
                        border-radius: 50%;
                        border-style: dashed;
                        border-color: gray;
                        position: absolute;
                        border-width: 1px;
                        /*margin: 100px;*/
                        /*transform-origin: -75px -75px;*/
                        /*animation: rotate 4s infinite linear;*/
                        margin: 0;
                        padding: 0;
                    }

                    /*土星*/
                    .saturn {
                        left: 92px;
                        top: 387px;
                        height: 26px;
                        width: 26px;
                        background-color: rgb(215, 171, 68);
                        border-radius: 50%;
                        position: absolute;
                        transform-origin: 308px 13px;
                        animation: rotate 183.92s infinite linear;
                    }

                    /*土星轨道*/
                    .saturnOrbit {
                        left: 105px;
                        top: 105px;
                        height: 590px;
                        width: 590px;
                        background-color: transparent;
                        border-radius: 50%;
                        border-style: dashed;
                        border-color: gray;
                        position: absolute;
                        border-width: 1px;
                        /*margin: 100px;*/
                        /*transform-origin: -75px -75px;*/
                        /*animation: rotate 4s infinite linear;*/
                        margin: 0;
                        padding: 0;
                    }

                    /*天王星*/
                    .uranus {
                        left: 41.5px;
                        top: 386.5px;
                        height: 27px;
                        width: 27px;
                        background-color: rgb(164, 192, 206);
                        border-radius: 50%;
                        position: absolute;
                        transform-origin: 358.5px 13.5px;
                        animation: rotate 524.46s infinite linear;
                    }

                    /*天王星轨道*/
                    .uranusOrbit {
                        left: 55px;
                        top: 55px;
                        height: 690px;
                        width: 690px;
                        background-color: transparent;
                        border-radius: 50%;
                        border-style: dashed;
                        border-color: gray;
                        position: absolute;
                        border-width: 1px;
                        /*margin: 100px;*/
                        /*transform-origin: -75px -75px;*/
                        /*animation: rotate 4s infinite linear;*/
                        margin: 0;
                        padding: 0;
                    }

                    /*海王星*/
                    .neptune {
                        left: 10px;
                        top: 390px;
                        height: 20px;
                        width: 20px;
                        background-color: rgb(133, 136, 180);
                        border-radius: 50%;
                        position: absolute;
                        transform-origin: 390px 10px;
                        animation: rotate 1028.76s infinite linear;
                    }

                    /*海王星轨道*/
                    .neptuneOrbit {
                        left: 20px;
                        top: 20px;
                        height: 760px;
                        width: 760px;
                        background-color: transparent;
                        border-radius: 50%;
                        border-style: dashed;
                        border-color: gray;
                        position: absolute;
                        border-width: 1px;
                        /*margin: 100px;*/
                        /*transform-origin: -75px -75px;*/
                        /*animation: rotate 4s infinite linear;*/
                        margin: 0;
                        padding: 0;
                    }

                    @keyframes rotate {
                        100% {
                            transform: rotate(-360deg);
                        }
                    }
                </style>
View Code

 

快捷键大全

canvas

标签:round   缩放   重复   lte   容器   pos   空白   init   head   

原文地址:https://www.cnblogs.com/caozhenghua/p/11876982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!