HTML5 Audio (Audio)
Definition and Usage
<audio src="someaudio.wav" controls="controls"> 您的浏览器不支持 audio 标签。 </audio>
Tip: You can place the text between the start and end tags, so that older browsers can display the information does not support the tag.
MIME type of audio format
Format |
MIME-type |
MP3 |
audio/mpeg |
Ogg |
audio / ogg |
Wav |
audio/wav |
Attributes
Attributes |
value |
description |
autoplay |
autoplay |
If this attribute is present, the audio playback immediately after the ready. |
controls |
controls |
If this attribute is present, then displayed to the user controls, such as play button. |
loop |
loop |
If this attribute is present, then whenever the audio end start playing again. |
muted |
muted |
The provisions of the video output should be muted. |
preload |
preload |
If this attribute is present, the audio is loaded when the page is loaded and ready to play. If you use the "autoplay", this attribute is ignored. |
src |
url |
URL you want to play audio. |
Minimum Demo Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>audio</title>
</head>
<body>
<audio src="someaudio.wav" controls="controls"> 您的浏览器不支持 audio 标签。 </audio>
</body>
</html>
src can modify their own local path, please pay attention to adjust the volume and then play
"Light" Chen grain
HTML5 Video (Video)
<video src="movie.mp4" controls="controls"> 您的浏览器不支持 video 标签。 </video>
Video Format
format |
MIME-type |
MP4 |
video/mp4 |
WebM |
video/webm |
Ogg |
video / ogg |
HTML5 Audio / Video method
method |
description |
addTextTrack() |
/ Video add new text to an audio track |
canPlayType() |
Detecting whether the browser can play the specified audio / video type |
load() |
Reload the audio / video elements |
play() |
Start playing the audio / video |
pause() |
Pause the audio / video currently playing |
Just some common method, used in more specific further investigation.
Minimum Demo Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<video src="video.mp4" controls="controls"> 您的浏览器不支持 video 标签。 </video>
</body>
</html>
HTML5 Canvas
HTML5 canvas element is used to draw graphics, done by a script (usually JavaScript).
<canvas id="myCanvas" width="200" height="100" style="border:1px solid #000000;"> </canvas>
Create a canvas element, and then to add a border using style attributes.
Colors, patterns and shadows
Attributes |
description |
fillStyle |
Or returns set colors, gradients, or patterns for filling the drawing |
strokeStyle |
Or returns the color set for the stroke, or gradient mode |
shadowColor |
Provided for color shading or returns |
shadowBlur |
Sets or returns the level of blur for shadow |
shadowOffsetX |
Or returns from the shape of the shadow set horizontal distance |
shadowOffsetY |
Sets or returns from the shape of the shadow vertical distance |
method |
description |
createLinearGradient() |
Create a linear gradient (with the canvas content) |
createPattern() |
Repeated the specified elements in a specified direction |
createRadialGradient() |
Create a radial / circular gradient (with the canvas content) |
addColorStop() |
Gradient color specified in the object and stop position |
Canvas - Gradient
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Canvas</title>
</head>
<body>
<div>
<div> 线性渐变:</div>
<canvas id="myCanvas" width="200" height="200"></canvas>
</div>
<div>
<div> 径向/圆渐变:</div>
<canvas id="myCanvas2" width="200" height="200"></canvas>
</div>
<script>
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
// 创建渐变
var grd=ctx.createLinearGradient(0,0,200,0);
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");
// 填充渐变
ctx.fillStyle=grd;
ctx.fillRect(10,10,150,80);
var c=document.getElementById("myCanvas2");
var ctx=c.getContext("2d");
// 创建渐变
var grd=ctx.createRadialGradient(75,50,5,90,60,100);
grd.addColorStop(0,"red");
grd.addColorStop(1,"white");
// 填充渐变
ctx.fillStyle=grd;
ctx.fillRect(10,10,150,80);
</script>
</body>
</html>
Line style
Attributes |
description |
lineCap |
Sets or returns the line end cap style |
lineJoin |
When two lines intersect or returns set, the type of corner created |
lineWidth |
Or returns the currently set line width |
miterLimit |
Sets or returns the maximum length of the miter |
rectangle
method |
description |
rect() |
Create a rectangle |
fillRect() |
Draw "is filled with" rectangle |
strokeRect() |
Drawing a rectangle (no fill) |
clearRect() |
Clear the pixels within a given rectangle |
path
method |
description |
fill() |
Filling current drawing (path) |
stroke() |
Draw a path defined |
beginning path () |
Starting a path, or reset the current path |
moveTo() |
The path to the specified point in the canvas, do not create lines |
closePath() |
Created from the current point back to the starting point of the path |
lineTo() |
Add a new point, and then create a line from that point to the last point specified in the canvas |
clip() |
Cut any shape and size from the original canvas area |
quadraticCurveTo () |
Create a quadratic Bezier curve |
bezierCurveTo() |
Create cubic Bezier curves |
arc() |
Create arc / curve (to create the round or circular section) |
arcTo() |
Create arc / curve between two tangents |
isPointInPath() |
If the specified point in the current path, it returns true, false otherwise |
Canvas - Path
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>video</title>
</head>
<body>
<canvas id="myCanvas" width="200" height="100"
style="border:1px solid #000000;">
</canvas>
<script>
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.beginPath();
ctx.arc(95,50,40,0,2*Math.PI);
ctx.stroke();
</script>
</body>
</html>
Change
method |
description |
scale() |
Scales the current drawing to a larger or smaller |
rotate() |
Rotate the current drawing |
translate() |
Remapping (0,0) position on the canvas |
transform() |
Alternatively drawing current transformation matrix |
set trans form () |
Will reset to the current conversion matrix. Then run transform () |
text
Attributes |
description |
font |
Sets or returns the text content of the current font attributes |
textAlign |
Sets or returns the current text alignment |
text baseline |
设置或返回在绘制文本时使用的当前文本基线 |
方法 |
描述 |
fillText() |
在画布上绘制“被填充的”文本 |
strokeText() |
在画布上绘制文本(无填充) |
measureText() |
返回包含指定文本宽度的对象 |
Canvas - 文本
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Canvas</title>
</head>
<body>
<canvas id="myCanvas" width="200" height="200"></canvas>
<script>
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
ctx.font="30px Arial";
ctx.fillText("小橘子ღ",10,100);
ctx.strokeText("Hi~ Canvas",10,50);
</script>
</body>
</html>
图像绘制
Canvas - 图像
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Canvas</title>
</head>
<body>
<img id="scream" src="https://pic.cnblogs.com/avatar/1979230/20200321083100.png" alt="小橘子ღ">
<canvas id="myCanvas" width="200" height="200" style="border:1px solid #000000;"> 您的浏览器不支持 canvas 标签。</canvas>
<script>
var c=document.getElementById("myCanvas");
var ctx=c.getContext("2d");
var img = document.getElementById("scream");
ctx.drawImage(img, 10, 10)//若不显示画像则注释这行,取消注释下面三行
// img.onload = function(){
// ctx.drawImage(img, 10, 10)
// }
</script>
</body>
</html>
注意:这里有个坑,考虑到图片是从网络加载,如果 drawImage 的时候图片还没有完全加载完成,则什么都不做。即绘图失败。
所以我们应该保证在 img 绘制完成之后再 drawImage。
var img = new Image(); // 创建img元素
img.src = 'https://pic.cnblogs.com/avatar/1979230/20200321083100.png'; // 设置图片源地址
img.onload = function(){
ctx.drawImage(img, 0, 0)
}
像素操作
属性 |
描述 |
width |
返回 ImageData 对象的宽度 |
height |
返回 ImageData 对象的高度 |
data |
返回一个对象,其包含指定的 ImageData 对象的图像数据 |
方法 |
描述 |
createImageData() |
创建新的、空白的 ImageData 对象 |
getImageData() |
返回 ImageData 对象,该对象为画布上指定的矩形复制像素数据 |
putImageData() |
把图像数据(从指定的 ImageData 对象)放回画布上 |
合成
属性 |
描述 |
globalAlpha |
设置或返回绘图的当前 alpha 或透明值 |
globalCompositeOperation |
设置或返回新图像如何绘制到已有的图像上 |
进阶操作