萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網頁製作 >> HTML5 教學

HTML5 仿 iPhone 開機畫面:LufyLegend 引擎實現全過程

用 HTML5 + LufyLegend 引擎復刻 iOS 開機畫面:金屬機身、動態壁紙、鎖屏時間、滑動解鎖滑塊。純 Web 技術還原原生體驗,展示 Canvas 遊戲引擎在 UI 重現上的表現力。

效果預覽

iPhone 開機畫面仿製效果

線上體驗:https://www.cnblogs.com/yorhom/articles/3163078.html

架構拆解

入口 index.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>iPhone Boot</title>
  <script src="./lufylegend-1.7.7.min.js"></script>
  <script src="./js/Main.js"></script>
</head>
<body>
  <div id="mylegend">loading...</div>
</body>
</html>

主邏輯 Main.js

init(50, "mylegend", 450, 640, main); // 50fps, 容器, 寬, 高, 入口
LGlobal.setDebug(true);

const loadData = [
  {path: "./js/Shape.js", type: "js"},
  {path: "./js/BootPage.js", type: "js"},
  {name: "wallpaper", path: "./images/wall_paper.jpg"}
];
let datalist = {}, backLayer, iosShape, bootPage;

function main() {
  LLoadManage.load(loadData, null, gameInit);
}
function gameInit(result) {
  datalist = result;
  initLayer();
  addShape();
  addBack();
}
function initLayer() {
  backLayer = new LSprite();
  addChild(backLayer);
}
function addShape() {
  iosShape = new Shape("IPHONE", 400, 600);
  iosShape.x = 15; iosShape.y = 5;
  backLayer.addChild(iosShape);
}
function addBack() {
  bootPage = new BootPage();
  bootPage.x = 40; bootPage.y = 40;
  const w = iosShape.getScreenWidth();
  const h = iosShape.getScreenHeight();
  bootPage.addWallPaper(new LBitmapData(datalist["wallpaper"], 200, 480, w, h));
  bootPage.addTime();
  bootPage.addSlider();
  iosShape.addChild(bootPage);
}

Shape.js:手機外殼與螢幕繪製

function Shape(type, w, h) {
  base(this, LSprite, []);
  this.deviceWidth = w; this.deviceHeight = h; this.type = type;
  this.shapeLayer = new LSprite(); this.addChild(this.shapeLayer); // 外殼層
  this.homeButtonLayer = new LSprite(); this.addChild(this.homeButtonLayer); // Home鍵層
  this.screenLayer = new LSprite(); this.addChild(this.screenLayer); // 螢幕層
  this._showSelf();
}
Shape.prototype._showSelf = function() {
  if(this.type === "IPHONE") {
    // 外殼圓角矩形 + 陰影
    const shadow = new LDropShadowFilter(15, 45, "black", 20);
    this.shapeLayer.graphics.drawRoundRect(10, "black", [0,0,this.deviceWidth,this.deviceHeight,15], true, "black");
    this.shapeLayer.filters = [shadow];
    // 螢幕區域
    this.screenLayer.graphics.drawRect(0, "black", [this.deviceWidth/10, this.deviceWidth/10, this.deviceWidth*0.8, this.deviceHeight*0.8], true, "white");
    // Home 鍵:圓形 + 圓角方塊
    this.homeButtonLayer.graphics.drawArc(1, "black", [this.deviceWidth/2, this.deviceHeight*0.87+this.deviceWidth/10, this.deviceWidth/16, 0, 2*Math.PI], true, "#191818");
    this.homeButtonLayer.graphics.drawRoundRect(3, "white", [this.deviceWidth/2-10, this.deviceHeight*0.87+this.deviceWidth/10-10, 20,20,5]);
  }
};
Shape.prototype.getScreenWidth = function() { return this.deviceWidth * 0.8; };
Shape.prototype.getScreenHeight = function() { return this.deviceHeight * 0.8; };

BootPage.js:鎖屏界面邏輯

  • 壁紙:LBitmap 繪製,縮放填滿螢幕層
  • 時間/日期:LTextField + ENTER_FRAME 事件每幀更新,陰影濾鏡增強可讀性
  • 滑動解鎖:滑塊層監聽 MOUSE_DOWN/MOVE/UP,拖動跟隨手指,釋放時判斷距離:足夠 → 解鎖動畫;不足 → 彈回彈性動畫 (LTweenLite)
BootPage.prototype.addSlider = function() {
  this.sliderLayer = new LSprite(); this.addChild(this.sliderLayer);
  // 背景條
  const barBorder = new LSprite(); barBorder.x = 35; barBorder.y = this.getScreenHeight()-70;
  this.addChild(barBorder);
  // 提示文字
  const txt = new LTextField(); txt.size=12; txt.x=80; txt.y=10; txt.color="white"; txt.text="Slide to unlock.";
  barBorder.addChild(txt);
  // 滑塊
  const bar = new LSprite(); bar.x = 35; bar.y = this.getScreenHeight()-70; bar.canMoveBar = false;
  bar.addEventListener(LMouseEvent.MOUSE_DOWN, e => { bar.canMoveBar = true; });
  bar.addEventListener(LMouseEvent.MOUSE_UP, e => {
    if (bar.x > 200) { // 解鎖閾值
      LTweenLite.to(bar, 0.5, {x: 300, onComplete: () => this.unlock()});
    } else {
      LTweenLite.to(bar, 0.5, {x: 35}); // 彈回
    }
    bar.canMoveBar = false;
  });
  bar.addEventListener(LMouseEvent.MOUSE_MOVE, e => {
    if (bar.canMoveBar) bar.x = e.selfX - 35;
  });
  this.addChild(bar);
};

技術亮點

  1. 分層架構:外殼/螢幕/Home鍵/鎖屏內容分層,職責清晰
  2. 資源預載:LLoadManage 統一管理圖片/JS 模組載入
  3. 時間驅動:ENTER_FRAME 實現時鐘即時更新
  4. 觸控/滑鼠統一:LufyLegend 抽象事件模型,桌面/移動端代碼復用
  5. 補間動畫:LTweenLite 處理滑塊彈回/解鎖滑出動畫

此專案展示 HTML5 遊戲引擎構建高保真 UI 原型的可行性,代碼結構清晰,易於擴充完整 iOS 模擬器雛形。

copyright © 萬盛學電腦網 all rights reserved