Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
.說明:
推薦指數:★★★★
通過動畫太極的方法,增加學習興趣,對html的結構和css、JavaScript、div的認識和了解會逐步深入。
2.復習html的結構框架
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>html結構基本框架代碼</title>
</head>
<body>
</body>
</html>
3 div法
3.1 代碼:復制下面的代碼,命名為:div法.html,用瀏覽器打開即可。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>div法的旋轉的太極圖</title>
</head>
<!--單獨style,不在head和body,只是在body內有一個div容器-->
<style>
div{
width: 0;
/*這個高就是黑色圓形和白色圓形的直徑和*/
height: 200px;
/*黑色太極部分的圈帶動的黑色的陰影*/
border-left: 100px solid black;
/*白色太極部分的圈帶動的白色的陰影*/
border-right: 100px solid #fff;
box-shadow: 0 0 15px rgba(0,0,0,.5);
/*旋轉半徑100*/
border-radius: 100px;
/*旋轉速度定義,越小越快*/
-webkit-animation:rotation 2.5s linear infinite;
}
div:before{
content: "";
position: absolute;
height: 100px;
z-index: 1;
border-radius: 100px;
/*白色的小半圓*/
border-left: 50px solid #fff;
border-right: 50px solid #fff;
left: -50px;
/*黑色的小半圓,因為轉動拖動黑色陰影*/
box-shadow: 0 100px 0 black;
}
div:after{
content: "";
position: absolute;
/*height是太極里面小圓圈的高30,要和border-radius30一致,才畫出圓*/
height: 30px;
/*這個是顯示小圓圈的,0就是不顯示*/
z-index: 1;
border-radius: 30px;
border-left: 15px solid;
border-right: 15px solid;
/*top和left,決定小圓圈白色和黑色的位置*/
top: 40px;
left: -15px;
/*黑色太極部分里面的小白色圓圈*/
box-shadow: 0 100px 0 white;
}
/*旋轉角度函數定義*/
@-webkit-keyframes rotation {
0% {-webkit-transform:rotate(0deg);}
100% {-webkit-transform:rotate(-360deg);}
}
</style>
<body>
<div></div>
</body>
</html>
3.2 效果圖
4 css法
4.1 css法.html代碼
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>css法的旋轉的太極圖</title>
<!--css導入和js導入不一樣,請注意-->
<!--script-- src="./tj.css"></!--script-->
<link rel="stylesheet" type="text/css" href="./tj.css">
</head>
<body>
<div class="tj"></div>
</body>
</html>
4.2 tj.css代碼:注意與上面兩個文件放在同一個文件夾下
.tj{
width: 100px;
height: 200px;
border: solid black;
border-width: 2px 100px 2px 2px;
background-color: #fff;
border-radius: 50%;
position: absolute;
/*run是動起來的函數,在最后面設置和定義*/
animation: run 2s linear infinite;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
}
.tj:before{
content: " ";
position: absolute;
width: 28px;
height: 28px;
background-color: black;
/*36=(100-28)/2得到的,是小白色圓圈的半徑*/
border: 36px #ffffff solid;
border-radius: 50%;
top: 0;
left: 50%;
}
.tj:after{
content: " ";
position: absolute;
width: 28px;
height: 28px;
background-color: #ffffff;
/*36=(100-28)/2得到的,是小黑色圓圈的半徑*/
border: 36px black solid;
border-radius: 50%;
top: 50%;
left: 50%;
}
/*run動起來的函數定義*/
@keyframes run{
0%{
transform: rotate(0deg);
}
100%{
transform: rotate(360deg);
}
}
4.3 效果圖
5 js法=就是JavaScript法
5.1 js法.html代碼:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>js法的旋轉的太極圖</title>
<!--注意下面2鐘都可以,主要是瀏覽器都支持html5-->
<!--script-- src="script.js" type="text/javascript"></!--script-->
<script src="./script.js"></script>
<!--簡單的css內容就這樣寫在下面,如果css比較復雜,則需要外部導入-->
<style type="text/css">
canvas{
display: block;
margin: 20px auto;
}
</style>
</head>
<body onload="main()">
<!--畫布大小,畫布框的線顏色藍色設置,solid blue是指畫布外框的顏色為藍色-->
<canvas width="300" height="300" id="canvas"style="border:1px solid blue"></canvas>
</body>
</html>
5.2 script.js代碼:與上面html放在同一個文件夾下
//注意到沒有null=0,效果是一樣的
var angle = 0;
//var canvas = null;
//var ctx = null;
var canvas = 0;
var ctx = 0;
function main()
{
window.setInterval(function()
{
canvas = document.getElementById("canvas");
ctx = canvas.getContext("2d");
// 畫布大小有關
ctx.clearRect(0, 0, 300, 300);
// 線條寬度0~10,均可
ctx.lineWidth = 0;
ctx.save();
// 旋轉的中心點的坐標位置150,150
ctx.translate(150,150);
ctx.rotate(angle);
// 太極黑色部分
ctx.fillStyle = "black";
ctx.beginPath();
// 注意幾個函數數值的關系,120,60,半徑和坐標的關系,如果要縮小半徑,那么坐標也需要調整
ctx.arc(0, 0, 120, 0, Math.PI, true);
ctx.fill();
ctx.closePath();
// 太極白色部分
ctx.fillStyle = "white";
ctx.beginPath();
ctx.arc(0, 0, 120, 0, Math.PI, false);
ctx.fill();
ctx.closePath();
// 太極黑色部分
ctx.fillStyle = "black";
ctx.beginPath();
ctx.arc(60, -0.6, 60, 0, Math.PI, false);
ctx.fill();
ctx.closePath();
// 太極白色部分
ctx.fillStyle = "white";
ctx.lineWidth = 0;
ctx.beginPath();
ctx.arc(-60, 0, 60, 0, Math.PI, true);
ctx.fill();
ctx.closePath();
// 白色太極部分里面的小黑色圓圈
ctx.fillStyle = "black";
ctx.beginPath();
//畫圓的函數:-145,0是坐標,15是半徑,2*Math.PI是一個圓,一個π是半圓
ctx.arc(-60, 0, 15, 0, 2*Math.PI, false);
ctx.fill();
ctx.closePath();
// 黑色太極部分里面的小白色圓圈
ctx.fillStyle = "white";
ctx.beginPath();
ctx.arc(60, 0, 15, 0, 2*Math.PI, false);
ctx.fill();
ctx.closePath();
// 旋轉角度一次增加多少
ctx.restore();
angle += 0.02;
// 50代表轉速,越大越慢,越小越快
},1);
}
5.3 效果圖
6 值得收藏,慢慢回味。
現效果視頻:
https://m.toutiaoimg.com/i7012628289806139918/?gd_ext_json=%7B%22enter_from%22%3A%22click_creation_center%22%2C%22category_name%22%3A%22creation_center%22%7D&enter_from=click_creation_center&category_name=creation_center&share_token=c3b59c5c-c95d-43a9-a842-4cd30e321a34&tt_from=copy_link&utm_source=copy_link&utm_medium=toutiao_android&utm_campaign=client_share
實現代碼:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>旋轉、縮放</title>
<style type="text/css">
.box {
width: 750px;
height: 520px;
margin: 50px auto;
background-image: url(狼王-靈夢狼王.jpg);
position: relative;
/* 溢出隱藏 */
overflow: hidden;
}
.box img {
/* 設置圖片位置 */
position: absolute;
top: 0;
left: 0;
}
.img1 {
z-index: 100;
/* 動畫 */
animation: image1 2s linear 1s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image1 {
0% {
transform: scale(1);
}
50% {
/* 縮放,縮小 */
transform: scale(0.5);
}
100% {
transform: scale(0.0001);
}
}
.img2 {
z-index: 98;
/* 動畫 */
animation: image2 2s linear 3s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image2 {
0% {
transform: scale(1);
}
50% {
/* 縮放,縮小 */
transform: scale(1.5);
/* 設置不透明度 */
opacity: 1;
}
100% {
transform: scale(5);
opacity: 0;
}
}
.img3 {
z-index: 97;
/* 動畫 */
animation: image3 2s linear 5s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image3 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(180deg);
/* 設置不透明度 */
opacity: 1;
}
100% {
transform: rotate(360deg);
opacity: 0;
}
}
.img4 {
z-index: 96;
/* 動畫 */
animation: image4 2s linear 7s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image4 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(-180deg);
/* 設置不透明度 */
opacity: 1;
}
100% {
transform: rotate(-360deg);
opacity: 0;
}
}
.img5 {
z-index: 95;
/* 動畫 */
animation: image5 2s linear 9s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image5 {
0% {
/* 繞y軸旋轉 */
transform: rotateY(0deg);
}
50% {
transform: rotateY(-90deg);
/* 設置不透明度 */
opacity: 1;
}
100% {
transform: rotateY(-180deg);
opacity: 0;
}
}
.img6 {
z-index: 94;
/* 動畫 */
animation: image6 2s linear 11s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image6 {
0% {
transform: rotateY(0deg);
}
50% {
transform: rotateY(90deg);
/* 設置不透明度 */
opacity: 1;
}
100% {
transform: rotateY(180deg);
opacity: 0;
}
}
.img7 {
z-index: 93;
/* 動畫 */
animation: image7 2s linear 13s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image7 {
0% {
transform: rotateZ(0deg);
}
50% {
transform: rotateZ(180deg);
/* 設置不透明度 */
opacity: 1;
}
100% {
transform: rotateZ(360deg);
opacity: 0;
}
}
.img8 {
z-index: 92;
/* 動畫 */
animation: image8 2s linear 15s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image8 {
0% {
transform: rotateZ(0deg);
}
50% {
transform: rotateZ(-180deg);
/* 設置不透明度 */
opacity: 1;
}
100% {
transform: rotateZ(-360deg);
opacity: 0;
}
}
.img9 {
z-index: 91;
/* 動畫 */
animation: image9 2s linear 17s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image9 {
0% {
transform: rotateX(0deg);
}
50% {
transform: rotateX(-90deg);
/* 設置不透明度 */
opacity: 1;
}
100% {
transform: rotateX(-180deg);
opacity: 0;
}
}
.img10 {
z-index: 90;
/* 動畫 */
animation: image10 2s linear 19s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image10 {
0% {
transform: rotateX(0deg);
}
50% {
transform: rotateX(90deg);
/* 設置不透明度 */
opacity: 1;
}
100% {
transform: rotateX(180deg);
opacity: 0;
}
}
.img11 {
z-index: 89;
/* 動畫 */
animation: image11 2s linear 21s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: top;
}
@keyframes image11 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(90deg);
}
100% {
transform: rotate(180deg);
}
}
.img12 {
z-index: 88;
/* 動畫 */
animation: image12 2s linear 23s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: top;
}
@keyframes image12 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(-90deg);
}
100% {
transform: rotate(-180deg);
}
}
.img13 {
z-index: 87;
/* 動畫 */
animation: image13 2s linear 25s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: top;
}
@keyframes image13 {
0% {
transform: rotateX(0deg);
}
50% {
transform: rotateX(-45deg);
}
100% {
transform: rotateX(-90deg);
}
}
.img14 {
z-index: 86;
/* 動畫 */
animation: image14 2s linear 27s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: top;
}
@keyframes image14 {
0% {
transform: rotateX(0deg);
/* 設置模糊度 */
filter: blur(0px);
}
50% {
transform: rotateX(45deg);
}
100% {
transform: rotateX(90deg);
filter: blur(1);
}
}
.img15 {
z-index: 85;
/* 動畫 */
animation: image15 2s linear 29s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: bottom;
}
@keyframes image15 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(-90deg);
}
100% {
transform: rotate(-180deg);
}
}
.img16 {
z-index: 84;
/* 動畫 */
animation: image16 2s linear 31s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: bottom;
}
@keyframes image16 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(90deg);
}
100% {
transform: rotate(180deg);
}
}
.img17 {
z-index: 83;
/* 動畫 */
animation: image17 2s linear 33s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: bottom;
}
@keyframes image17 {
0% {
transform: rotateX(0deg);
}
50% {
transform: rotateX(45deg);
}
100% {
transform: rotateX(90deg);
}
}
.img18 {
z-index: 82;
/* 動畫 */
animation: image18 2s linear 35s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: bottom;
}
@keyframes image18 {
0% {
transform: rotateX(0deg);
filter: blur(0px);
}
50% {
transform: rotateX(-45deg);
}
100% {
transform: rotateX(-90deg);
filter: blur(1px);
}
}
.img19 {
z-index: 81;
/* 動畫 */
animation: image19 2s linear 37s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: left;
}
@keyframes image19 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(-90deg);
}
100% {
transform: rotate(-180deg);
}
}
.img20 {
z-index: 80;
/* 動畫 */
animation: image20 2s linear 39s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: left;
}
@keyframes image20 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(90deg);
}
100% {
transform: rotate(180deg);
}
}
.img21 {
z-index: 79;
/* 動畫 */
animation: image21 2s linear 41s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: left;
}
@keyframes image21 {
0% {
transform: rotateY(0deg);
}
50% {
transform: rotateY(45deg);
}
100% {
transform: rotateY(90deg);
}
}
.img22 {
z-index: 78;
/* 動畫 */
animation: image22 2s linear 43s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: left;
}
@keyframes image22 {
0% {
transform: rotateY(0deg);
filter: blur(0px);
}
50% {
transform: rotateY(-45deg);
}
100% {
transform: rotateY(-90deg);
filter: blur(1px);
}
}
.img23 {
z-index: 77;
/* 動畫 */
animation: image23 2s linear 45s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: right;
}
@keyframes image23 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(-90deg);
}
100% {
transform: rotate(-180deg);
}
}
.img24 {
z-index: 76;
/* 動畫 */
animation: image24 2s linear 47s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: right;
}
@keyframes image24 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(90deg);
}
100% {
transform: rotate(180deg);
}
}
.img25 {
z-index: 75;
/* 動畫 */
animation: image25 2s linear 49s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: right;
}
@keyframes image25 {
0% {
transform: rotateY(0deg);
filter: blur(0px);
}
50% {
transform: rotateY(45deg);
}
100% {
transform: rotateY(90deg);
filter: blur(1px);
}
}
.img26 {
z-index: 74;
/* 動畫 */
animation: image26 2s linear 51s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: right;
}
@keyframes image26 {
0% {
transform: rotateY(0deg);
}
50% {
transform: rotateY(-45deg);
}
100% {
transform: rotateY(-90deg);
}
}
.img27 {
z-index: 73;
/* 動畫 */
animation: image27 2s linear 53s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: left top;
}
@keyframes image27 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(90deg);
}
100% {
transform: rotate(180deg);
}
}
.img28 {
z-index: 72;
/* 動畫 */
animation: image28 2s linear 55s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
/* 設置旋轉原點 */
transform-origin: right top;
}
@keyframes image28 {
0% {
transform: rotate(0deg);
}
50% {
transform: rotate(-90deg);
}
100% {
transform: rotate(-180deg);
}
}
.img29 {
z-index: 71;
/* 動畫 */
animation: image29 2s linear 57s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image29 {
0% {
transform: rotateZ(0deg) scale(1);
}
50% {
transform: rotateZ(180deg) scale(0.5);
}
100% {
transform: rotateZ(360deg) scale(0.0001);
}
}
.img30 {
z-index: 70;
/* 動畫 */
animation: image30 2s linear 59s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image30 {
0% {
transform: rotateX(0deg) scale(1);
}
50% {
transform: rotateX(180deg) scale(0.5);
}
100% {
transform: rotateX(360deg) scale(0.0001);
}
}
.img31 {
z-index: 69;
/* 動畫 */
animation: image31 2s linear 61s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image31 {
0% {
transform: rotateY(0deg) scale(1);
}
50% {
transform: rotateY(180deg) scale(0.5);
}
100% {
transform: rotateY(360deg) scale(0.0001);
}
}
.img32 {
z-index: 68;
/* 動畫 */
animation: image32 2s linear 63s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image32 {
0% {
transform: scaleX(1);
}
50% {
transform: scaleX(0.5);
}
100% {
transform: scaleX(0.0001);
}
}
.img33 {
z-index: 67;
/* 動畫 */
animation: image33 2s linear 65s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image33 {
0% {
transform: rotateY(0deg) scaleX(1);
}
50% {
transform: rotateY(180deg) scaleX(0.5);
}
100% {
transform: rotateY(360deg) scaleX(0.0001);
}
}
.img34 {
z-index: 66;
/* 動畫 */
animation: image34 2s linear 67s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image34 {
0% {
transform: scaleY(1);
}
50% {
transform: scaleY(0.5);
}
100% {
transform: scaleY(0);
}
}
.img35 {
z-index: 65;
/* 動畫 */
animation: image35 2s linear 69s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image35 {
0% {
transform: rotateY(0deg) scaleY(1);
}
50% {
transform: rotateY(180deg) scaleY(0.5);
}
100% {
transform: rotateY(360deg) scaleY(0);
}
}
.img36 {
z-index: 64;
/* 動畫 */
animation: image36 2s linear 71s;
/* 讓動畫停留在最后一幀,不回到開始處 */
animation-fill-mode: forwards;
}
@keyframes image36 {
0% {
transform: rotate(0deg) scaleY(1);
}
50% {
transform: rotate(180deg) scaleY(1.5);
opacity: 1;
}
100% {
transform: rotate(360deg) scaleY(5);
opacity: 0;
}
}
</style>
</head>
<body>
<div class="box">
<img class="img1" src="狼蛛.jpg" alt="" />
<img class="img2" src="狼蛛-紫魅毒姬.jpg" alt="" />
<img class="img3" src="洛神.jpg" alt="" />
<img class="img4" src="獵魔人.jpg" alt="" />
<img class="img5" src="獵魔人-蜂針魔女.jpg" alt="" />
<img class="img6" src="獵魔人-原力神槍.jpg" alt="" />
<img class="img7" src="獵魔人-摯愛甜心.jpg" alt="" />
<img class="img8" src="羅剎郡主.jpg" alt="" />
<img class="img9" src="羅剎郡主-玫紅冰晶.jpg" alt="" />
<img class="img10" src="羅剎郡主-耀世神皇.jpg" alt="" />
<img class="img11" src="羅剎郡主-櫻落飛翎.jpg" alt="" />
<img class="img12" src="洛神-北境仙姬.jpg" alt="" />
<img class="img13" src="洛神-飛羽女王.jpg" alt="" />
<img class="img14" src="洛神-驚鴻仙子.jpg" alt="" />
<img class="img15" src="綠野花仙.jpg" alt="" />
<img class="img16" src="綠野花仙-精靈公主.jpg" alt="" />
<img class="img17" src="綠野花仙-矩陣天翼.jpg" alt="" />
<img class="img18" src="綠野花仙-綠夢天仙.jpg" alt="" />
<img class="img19" src="魅魔公主.jpg" alt="" />
<img class="img20" src="魅魔公主-煉金魔女.jpg" alt="" />
<img class="img21" src="魅魔公主-夢有靈犀.jpg" alt="" />
<img class="img22" src="魅魔公主-星幻少女.jpg" alt="" />
<img class="img23" src="哪吒.jpg" alt="" />
<img class="img24" src="哪吒-飛輪公主.jpg" alt="" />
<img class="img25" src="哪吒-黃金威靈.jpg" alt="" />
<img class="img26" src="哪吒-輪刃審判.jpg" alt="" />
<img class="img27" src="哪吒-異界仙將.jpg" alt="" />
<img class="img28" src="聶小倩.jpg" alt="" />
<img class="img29" src="聶小倩-黛染幽情.jpg" alt="" />
<img class="img30" src="聶小倩-勾魂燈使.jpg" alt="" />
<img class="img31" src="聶小倩-海洋之心.jpg" alt="" />
<img class="img32" src="聶小倩-綠影魔仙.jpg" alt="" />
<img class="img33" src="聶小倩-仲夏清和.jpg" alt="" />
<img class="img34" src="女武神.jpg" alt="" />
<img class="img35" src="女武神-火羽流炎.jpg" alt="" />
<img class="img36" src="女武神-金槍戰神.jpg" alt="" />
</div>
</body>
</html>
實現效果視頻:
https://www.ixigua.com/i7012628289806139918/
小新 編譯自 Insight Data Blog
量子位 出品 | 公眾號 QbitAI
寫個網頁能有多麻煩?在大多數公司里,這項工作分為三步:
1. 產品經理完成用戶調研任務后,列出一系列技術要求;
2. 設計師根據這些要求來設計低保真原型,逐漸修改得到高保真原型和UI設計圖;
3. 工程師將這些設計圖實現為代碼,最終變成用戶使用的產品。
這么多環節,任何地方出一點問題,都會拉長開發周期。因此,不少公司,比如Airbnb已經開始用機器學習來提高這個過程的效率。
△ Airbnb內部的AI工具,從圖紙到代碼一步到位
看起來很美好,但Airbnb還沒公開該模型中端到端訓練的細節,以及手工設計的圖像特征對該模型的貢獻度。這是該公司特有的閉源解決方案專利,可能不會進行公開。
好在,一個叫Ashwin Kumar的程序員創建了一個開源版本,讓開發者/設計師的工作變得更簡單。
以下內容翻譯自他的博客:
理想上,這個模型可以根據網站設計的簡單手繪原型,很快地生成一個可用的HTML網站:
△ SketchCode模型利用手繪線框圖來生成HTML網站
事實上,上面例子就是利用訓練好的模型在測試集上生成的一個實際網站,代碼請訪問:https://github.com/ashnkumar/sketch-code。
目前要解決的問題屬于一種更廣泛的任務,叫做程序綜合(program synthesis),即自動生成工作源代碼。盡管很多程序綜合研究通過自然語言規范或執行追蹤法來生成代碼,但在當前任務中,我會充分利用源圖像,即給出的手繪線框圖來展開工作。
在機器學習中有一個十分熱門的研究領域,稱為圖像標注(image caption),目的是構建一種把圖像和文本連接在一起的模型,特別是用于生成源圖像內容的描述。
△ 圖像標注模型生成源圖像的文本描述
我從一篇pix2code論文和另一個應用這種方法的相關項目中獲得靈感,決定把我的任務按照圖像標注方式來實現,把繪制的網站線框圖作為輸入圖像,并將其相應的HTML代碼作為其輸出內容。
注:上段提到的兩個參考項目分別是
pix2code論文:https://arxiv.org/abs/1705.07962
floydhub教程:https://blog.floydhub.com/turning-design-mockups-into-code-with-deep-learning/?source=techstories.org
確定圖像標注方法后,理想中使用的訓練數據集會包含成千上萬對手繪線框圖和對應的HTML輸出代碼。但是,目前還沒有我想要的相關數據集,我只好為這個任務來創建數據集。
最開始,我嘗試了pix2code論文給出的開源數據集,該數據集由1750張綜合生成網站的截圖及其相應源代碼組成。
△ pix2code數據集中的生成網站圖片和源代碼
這是一個很好的數據集,有幾個有趣的地方:
該數據集中的每個生成網站都包含幾個簡單的輔助程序元素,如按鈕、文本框和DIV對象。盡管這意味著這個模型受限于將這些少數元素作為它的輸出內容,但是這些元素可通過選擇生成網絡來修改和擴展。這種方法應該很容易地推廣到更大的元素詞匯表。
每個樣本的源代碼都是由領域專用語言(DSL)的令牌組成,這是該論文作者為該任務所創建的。每個令牌對應于HTML和CSS的一個片段,且加入編譯器把DSL轉換為運行的HTML代碼。
為了修改我的任務數據集,我要讓網站圖像看起來像手工繪制出的。我嘗試使用Python中的OpenCV庫和PIL庫等工具對每張圖像進行修改,包括灰度轉換和輪廓檢測。
最終,我決定直接修改原始網站的CSS樣式表,通過執行以下操作:
1. 更改頁面上元素的邊框半徑來平滑按鈕和DIV對象的邊緣;
2. 模仿繪制的草圖來調整邊框的粗細,并添加陰影;
3. 將原有字體更改為類似手寫的字體;
最終實現的流程中還增加了一個步驟,通過添加傾斜、移動和旋轉來實現圖像增強,來模擬實際繪制草圖中的變化。
現在,我已經處理好數據集,接下來是構建模型。
我利用了圖像標注中使用的模型架構,該架構由三個主要部分組成:
1. 一種使用卷積神經網絡(CNN)的計算機視覺模型,從源圖像提取圖像特征;
2. 一種包含門控單元GRU的語言模型,對源代碼令牌序列進行編碼;
3. 一個解碼器模型,也屬于GRU單元,把前兩個步驟的輸出作為輸入,并預測序列中的下一個令牌。
△ 以令牌序列為輸入來訓練模型
為了訓練模型,我將源代碼拆分為令牌序列。模型的輸入為單個部分序列及它的源圖像,其標簽是文本中的下一個令牌。該模型使用交叉熵函數作為損失函數,將模型的下個預測令牌與實際的下個令牌進行比較。
在模型從頭開始生成代碼的過程中,該推理方式稍有不同。圖像仍然通過CNN網絡進行處理,但文本處理開始時僅采用一個啟動序列。在每個步驟中,模型對序列中輸出的下個預測令牌將會添加到當前輸入序列,并作為新的輸入序列送到模型中;重復此操作直到模型的預測令牌為,或該過程達到每個文本中令牌數目的預定義值。
當模型生成一組預測令牌后,編譯器就會將DSL令牌轉換為HTML代碼,這些HTML代碼可以在任何瀏覽器中運行。
我決定使用BLEU分數來評估模型。這是機器翻譯任務中常用的一種度量標準,通過在給定相同輸入的情況下,衡量機器生成的文本與人類可能產生內容的近似程度。
實際上,BLEU通過比較生成文本和參考文本的N元序列,以創建修改后的準確版本。它非常適用于這個項目,因為它會影響生成HTML代碼中的實際元素,以及它們之間的相互關系。
最棒的是,我還可以通過檢查生成的網站來比較當前的實際BLEU分數。
△ 觀察BLEU分數
當BLEU分數為1.0時,則說明給定源圖像后該模型能在正確位置設置合適的元素,而較低的BLEU分數這說明模型預測了錯誤元素或是把它們放在相對不合適的位置。我們最終模型在評估數據集上的BLEU分數為0.76。
后來,我還想到,由于該模型只生成當前頁面的框架,即文本的令牌,因此我可以在編譯過程中添加一個定制的CSS層,并立刻得到不同風格的生成網站。
△ 一個手繪圖生成多種風格的網頁
把風格定制和模型生成兩個過程分開,在使用模型時帶來了很多好處:
1.如果想要將SketchCode模型應用到自己公司的產品中,前端工程師可以直接使用該模型,只需更改一個CSS文件來匹配該公司的網頁設計風格;
2. 該模型內置的可擴展性,即通過單一源圖像,模型可以迅速編譯出多種不同的預定義風格,因此用戶可以設想出多種可能的網站風格,并在瀏覽器中瀏覽這些生成網頁。
受到圖像標注研究的啟發,SketchCode模型能夠在幾秒鐘內將手繪網站線框圖轉換為可用的HTML網站。
但是,該模型還存在一些問題,這也是我接下來可能的工作方向:
1. 由于這個模型只使用了16個元素進行訓練,所以它不能預測這些數據以外的令牌。下一步方向可能是使用更多元素來生成更多的網站樣本,包括網站圖片,下拉菜單和窗體,可參考啟動程序組件(https://getbootstrap.com/docs/4.0/components/buttons/)來獲得思路;
2. 在實際網站構建中,存在很多變化。創建一個能更好反映這種變化的訓練集,是提高生成效果的一種好方法,可以通過獲取更多網站的HTML/CSS代碼以及內容截圖來提高;
3. 手繪圖紙也存在很多CSS修改技巧無法捕捉到的變化。解決這個問題的一種好方法是使用生成對抗網絡GAN來創建更逼真的繪制網站圖像。
代碼:https://github.com/ashnkumar/sketch-code
原文:https://blog.insightdatascience.com/automated-front-end-development-using-deep-learning-3169dd086e82
— 完 —
誠摯招聘
量子位正在招募編輯/記者,工作地點在北京中關村。期待有才氣、有熱情的同學加入我們!相關細節,請在量子位公眾號(QbitAI)對話界面,回復“招聘”兩個字。
量子位 QbitAI · 頭條號簽約作者
?'?' ? 追蹤AI技術和產品新動態
*請認真填寫需求信息,我們會在24小時內與您取得聯系。