果圖:
所有代碼:
.none {
-webkit-filter: none;
filter: none;
}
.blur {
-webkit-filter: blur(3px);
filter: blur(3px);
}
.grayscale {
-webkit-filter: grayscale(1);
filter: grayscale(1);
}
.invert {
-webkit-filter: invert(1);
filter: invert(1);
}
.sepia {
-webkit-filter: sepia(1);
filter: sepia(1);
}
button#snapshot {
margin: 0 10px 25px 0;
width: 110px;
}
video {
object-fit: cover;
}
const filterSelect = document.querySelector('select#filter');
const video = window.video = document.querySelector('video');
filterSelect.onchange = function() {
video.className = filterSelect.value;
};
navigator.mediaDevices.getUserMedia({
audio: false,
video: true
}).then(handleSuccess).catch(handleError);
function handleSuccess(stream) {
video.srcObject = stream;
}
function handleError(error) {
console.log('navigator.MediaDevices.getUserMedia error: ', error.message, error.name);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="./index.css">
</head>
<body>
<video playsinline autoplay></video>
<label for="filter">Filter: </label>
<select id="filter">
<option value="none">None</option>
<option value="blur">Blur</option>
<option value="grayscale">Grayscale</option>
<option value="invert">Invert</option>
<option value="sepia">Sepia</option>
</select>
<script src="./index.js"></script>
</body>
</html>
們經常在一些頁面中遇見svg格式的繪圖,通常是一臉懵逼,別急,看完這篇文章你就能很好地了解它了。
SVG 意為可縮放矢量圖形(Scalable Vector Graphics),是使用 XML 來描述二維圖形和繪圖程序的語言,后綴是“.svg”。它的優點是圖像在放大或改變尺寸的情況下其圖形質量不會有所損失。
SVG 文件可通過以下標簽嵌入 HTML 文檔:<embed>、<object> 或者 <iframe>。但是在html5中,直接用內聯的方式嵌入,方便太多了。各種方式的嵌入如下:
// 使用 <embed> 標簽
<embed
src="rect.svg"
width="300"
height="100"
type="image/svg+xml"
pluginspage="http://www.adobe.com/svg/viewer/install/"
/>
// 使用 <object> 標簽
<object
data="rect.svg"
width="300"
height="100"
type="image/svg+xml"
codebase="http://www.adobe.com/svg/viewer/install/"
/>
// 使用 <iframe> 標簽
<iframe src="rect.svg" width="300" height="100"></iframe>
// 在 HTML5 中, SVG 元素直接嵌入 HTML 頁面中
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="190">
<polygon
points="100,10 40,180 190,60 10,60 160,180"
style="fill:lime;stroke:purple;stroke-width:5;fill-rule:evenodd;"
/>
</svg>
1.矩形
<svg>
<rect width="300" height="100" style="fill:rgb(0,0,255);stroke-width:1;stroke:rgb(0,0,0)"/>
</svg>
// width="矩形的寬度"。必需的。
// height="矩形的高度"。必需的。
// x="矩形的左上角的x軸"
// y="矩形的左上角的y軸"
// rx="x軸的半徑(round元素)"
// ry="y軸的半徑(round元素)"
// + 顯現屬性:Color, FillStroke, Graphics
2.圓形
<svg>
<circle cx="100" cy="50" r="40" stroke="black"
stroke-width="2" fill="red"/>
</svg>
// cx="圓的x軸坐標"
// cy="圓的y軸坐標"
// r="圓的半徑". 必需.
// + 顯現屬性:顏色,FillStroke,圖形
3.橢圓
<svg>
<ellipse cx="300" cy="80" rx="100" ry="50"
style="fill:yellow;stroke:purple;stroke-width:2"/>
</svg>
// cx="橢圓x軸坐標"
// cy="橢圓y軸坐標"
// rx="沿x軸橢圓形的半徑"。必需。
// ry="沿y軸長橢圓形的半徑"。必需。
// + 顯現屬性:顏色,FillStroke,圖形
4.直線
<svg>
<line x1="0" y1="0" x2="200" y2="200"
style="stroke:rgb(255,0,0);stroke-width:2"/>
</svg>
// x1="直線起始點x坐標"
// y1="直線起始點y坐標"
// x2="直線終點x坐標"
// y2="直線終點y坐標"
// + 顯現屬性:Color, FillStroke, Graphics, Markers
5.多邊形
<svg>
<polygon points="200,10 250,190 160,210"
style="fill:lime;stroke:purple;stroke-width:1"/>
</svg>
// points="多邊形的點。點的總數必須是偶數"。必需的。
// fill-rule="FillStroke演示屬性的部分"
// + 顯現屬性:Color, FillStroke, Graphics, Markers
6.多段線
<svg>
<polyline points="20,20 40,25 60,40 80,120 120,140 200,180"
style="fill:none;stroke:black;stroke-width:3" />
</svg>
// points=折線上的"點"。必需的。
// + 顯現屬性:Color, FillStroke, Graphics, Markers
7.路徑
<svg>
<path d="M150 0 L75 200 L225 200 Z" />
</svg>
// d="定義路徑指令"
// pathLength="如果存在,路徑將進行縮放,以便計算各點相當于此值的路徑長度"
// transform="轉換列表"
// + 顯現屬性:Color, FillStroke, Graphics, Markers
8.文本
<svg>
<text x="0" y="15" fill="red">這是文本</text>
</svg>
// x="列表的X -軸的位置。在文本中在第n個字符的位置在第n個x軸。如果后面存在額外的字符,耗盡他們最后一個字符之后放置的位置。 0是默認"
// y="列表的Y軸位置。(參考x)0是默認"
// dx="在字符的長度列表中移動相對最后繪制標志符號的絕對位置。(參考x)"
// dy="在字符的長度列表中移動相對最后繪制標志符號的絕對位置。(參考x)"
// rotate="一個旋轉的列表。第n個旋轉是第n個字符。附加字符沒有給出最后的旋轉值"
// textLength="SVG查看器將嘗試顯示文本之間的間距/或字形調整的文本目標長度。(默認:正常文本的長度)"
// lengthAdjust="告訴查看器,如果指定長度就嘗試進行調整用以呈現文本。這兩個值是'spacing'和'spacingAndGlyphs'"
// + 顯現屬性:Color, FillStroke, Graphics, FontSpecification, TextContentElements
主要分濾鏡和漸變效果。
<defs> 和 <filter>
所有互聯網的SVG濾鏡定義在<defs>元素中。<defs>元素定義短并含有特殊元素(如濾鏡)定義。<filter>標簽用來定義SVG濾鏡。<filter>標簽使用必需的id屬性來定義向圖形應用哪個濾鏡。
1.濾鏡可以做一些模糊的效果和陰影,效果有很多,也可以自己嘗試地做一做。
比如做個簡單的邊緣模糊效果。
<svg>
<defs>
<filter id="f1" x="0" y="0">
<feGaussianBlur in="SourceGraphic" stdDeviation="15" /> //<feGaussianBlur> 元素是用于創建模糊效果
</filter>
</defs>
<rect width="90" height="90" stroke="green" stroke-width="3" fill="green" filter="url(#f1)" />
</svg>
2.漸變有線性漸變,放射性漸變,下面是個線性漸變的例子。
<svg>
<defs>
<linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
</defs>
<rect width="200" height="90" fill="url(#grad1)" />
</svg>
// 把上面的
<linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
......
</linearGradient>
// 替換成
<radialGradient id="grad1" cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
<stop offset="0%" style="stop-color:rgb(255,255,255);stop-opacity:0" />
<stop offset="100%" style="stop-color:rgb(0,0,255);stop-opacity:1" />
</radialGradient>
// 變成放射性漸變
眾多示例:https://www.runoob.com/svg/svg-examples.html
SVG 是一種使用 XML 描述 2D 圖形的語言。 SVG DOM 中的每個元素都是可用的。它的繪制不依賴分辨率,可以為某個元素附加 JavaScript 事件處理器,不過事件添加過多會影響渲染速度。如果 SVG 對象的屬性發生變化,瀏覽器能夠自動重現圖形。最適合帶有大型渲染區域的應用程序(比如谷歌地圖)。
Canvas 通過 JavaScript 來繪制 2D 圖形。它的繪制是依賴分辨率的,不能單獨為其中元素添加事件。在 canvas 中,一旦圖形被繪制完成,它就不會繼續得到瀏覽器的關注。如果其位置發生變化,那么整個場景也需要重新繪制,包括任何或許已被圖形覆蓋的對象。能夠以 .png 或 .jpg 格式保存結果圖像。最適合圖像密集型的游戲,其中的許多對象會被頻繁重繪
*請認真填寫需求信息,我們會在24小時內與您取得聯系。