做了三年前端,你才知道10分钟就能实现一个PC版魔方游戏

导读:最近手头有点紧,看到很多小朋友都在玩魔方,又没有money买,只能自己用JavaScript+css实现一个,在电脑端就能开启魔方之旅。

 

目录

项目启动演示

项目目录

​源码地址 


项目启动演示

前端使用JavaScript和纯CSS实现翻转魔方,电脑端也可以玩魔方了

项目目录

index.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<title>翻转魔方</title>
<link rel="stylesheet" type="text/css" href="styles/cube.css">
<link rel="stylesheet" type="text/css" href="styles/doodle2.css">
<link rel="stylesheet" type="text/css" href="styles/certificate2.css"><!-- compiled & minified cube code -->
<script charset="utf-8" src="scripts/tween.r12.js"></script>
<script charset="utf-8" src="scripts/three.js"></script>
<script charset="utf-8" src="scripts/cuber.min.js"></script></head>
<body>
<div id="bg"></div>
<div id="container"></div>
<div id="fail"><img src="media/static-cube.png"></div>
<div id="uipanel" class="fadeTransition" style="display:none"><div id="movecounter">0</div><div id="buttonpanel"><div id="sharebubble" class="fadeTransition" style="opacity:0"><input id="shareshortlink" type="text" value="google.com/doodles" readonly/><div id="sharegplus" class="bubbleicon SPRITE_google_plus_64_2x"></div><div id="sharefacebook" class="bubbleicon SPRITE_facebook_64_2x"></div><div id="sharetwitter" class="bubbleicon SPRITE_twitter_64_2x"></div><div id="shareemail" class="bubbleicon SPRITE_email_64_2x"></div></div><div id="uibuttons"><div id="sharebutton" class="bubbleicon SPRITE_share_64_2x"></div><div id="helpbutton" class="bubbleicon SPRITE_help_64_2x"></div><div id="searchbutton" class="bubbleicon SPRITE_search_64_2x"></div></div></div><div id="helpbubble" class="bubble" style="display:none"><span class="pointerup SPRITE_carrot_grey_up_2x"></span><div id="helpimage" class="one"></div><div id="helptext" class="helptext"></div><div id="helpnext" class="helptext"></div><span class="pointerdown SPRITE_carrot_down_2x"></span><br></div>
</div>
<script charset="utf-8" src="scripts/iecss3d.js"></script>
<script charset="utf-8" src="scripts/ierenderer.js"></script>
<script charset="utf-8" src="scripts/deviceMotion.js"></script>
<script charset="utf-8" src="scripts/locked.js"></script>
<!-- Certificate js -->
<script charset="utf-8" src="scripts/textBox.js"></script>
<script charset="utf-8" src="scripts/ResizeableTextBox.3.js"></script>
<script charset="utf-8" src="scripts/divBox.js"></script>
<script charset="utf-8" src="scripts/frames.js"></script>
<script charset="utf-8" src="scripts/certificate.js"></script>
<script charset="utf-8" src="scripts/main3.js"></script></body>
</html>

cube.css

.cube {width:  100%;height: 100%;
}
.faceLabel {display: none;position: absolute;font-size: 60px;text-align: center;text-shadow: 0 0 24px rgba( 0, 0, 0, 0.3 );color: #FFF;
}
.cube .cubelet {width:  1em;height: 1em;position: absolute;box-sizing: border-box;
}
.cube /*.cubelet >*/ .face {position: absolute;width:  1em;height: 1em;background-color: #000;text-align: center;-webkit-backface-visibility: hidden;-moz-backface-visibility:    hidden;-o-backface-visibility:      hidden;backface-visibility:         hidden;padding: 0.05em;-moz-box-sizing: border-box;-webkit-box-sizing: border-box;-o-box-sizing: border-box;-ms-box-sizing: border-box;box-sizing: border-box;
}
.cube /*.cubelet >*/ .face.faceIntroverted { background-color: #000; }
.cube /*.cubelet >*/ .face.faceTransparent { background-color: transparent; }
.purty .face { opacity: 0.5; }
.purty .face.faceFront { background-color: #FFF; }
.purty .face.faceUp    { background-color: #E87000; }
.purty .face.faceRight { background-color: #3D81F6; }
.purty .face.faceDown  { background-color: #DC422F; }
.purty .face.faceLeft  { background-color: #009D54; }
.purty .face.faceBack  { background-color: #F5B400; }
.cube /*.cubelet > */.face > .wireframe {display: none;position: absolute;width:  140px;height: 140px;background-color: rgba( 255, 255, 255, 0.05 );
}
.cube .sticker {width:  100%;height: 100%;background-color: #F0F;border-radius: 0.1em;text-align: center;
}
.cube .sticker.red   { background: #DC422F; }
.cube .sticker.white  { background: #F3F3F3; }
.cube .sticker.blue  { background: #3D81F6;  }
.cube .sticker.green  { background: #009D54; }
.cube .sticker.orange  { background: #E87000;  }
.cube .sticker.yellow  { background: #F5B400;  }
.cube .sticker.stickerLogo {background: #F3F3F3 url( "../media/rubiksLogoClassic.png" );background-size: 100% 100%;background-repeat: none;
}
.cube /*.cubelet >*/ .face > .id {display: none;position: absolute;z-index: 1000;width:  90%;height: 90%;font-size:   1em;line-height: 0.9em;text-align: center;color: #000;
}
.cube /*.cubelet >*/ .face > .id > .underline {border-bottom: 1px solid #000;
}
.cube /*.cubelet > */.face.faceIntroverted > .id {color: #FEFEFE;
}
.cube /*.cubelet >*/ .face.faceIntroverted > .id > .underline {border-bottom: 1px solid #FEFEFE;
}
.cube /*.cubelet >*/ .face > .text {display: none;position: absolute;z-index: 2000;width:  100%;height: 100%;text-align: center;color: #FFF;font-size:   1em;line-height: 1em;
}

certificate.js

var cubeletsUnused = new ERNO.Group();
var certName, certSolved, certMovesNumber, certMovesHeader, certDate,certTimeNumber, certTimeHeader;
var lineLeft, lineRight;
var textBoxes = [];
var buttons = [];
var coords;
var frames = [];
var tweenSpeed = 600;
var allObjects = [];
var certificateCreated = false;
function doCertificate() {if (certificateCreated) {return;}certificateCreated = true;cube.mouseControlsEnabled = false;cube.keyboardControlsEnabled = false;document.getElementById('container').appendChild(cube.domElement);makeCertificate();
}
function animateCubeBack(callback) {var bg = document.getElementById('bg');var tweenCoords = {camZ: cube.camera.position.z,fov: cube.camera.fov,x: cube.position.x,y: cube.position.y,z: cube.position.z,xRot: cube.rotation.x,yRot: cube.rotation.y,zRot: cube.rotation.z,bgOpacity: 1}new TWEEN.Tween(tweenCoords).to({camZ: 2800,fov: 49,x: 0,y: 0,z: 0,xRot: (90).degreesToRadians(),yRot: (0).degreesToRadians(),zRot: (135).degreesToRadians(),bgOpacity: 0}, 1000).onUpdate(function() {cube.camera.position.z = tweenCoords.camZ;cube.camera.fov = tweenCoords.fov;cube.position.set(tweenCoords.x, tweenCoords.y, tweenCoords.z);cube.rotation.set(tweenCoords.xRot, tweenCoords.yRot, tweenCoords.zRot);bg.style.opacity = tweenCoords.bgOpacity;}).easing(TWEEN.Easing.Quartic.InOut).onComplete(function() {callback();}).start(cube.time);
}
function makeCertificate() {cube.hideInvisibleFaces = false;cube.showIntroverts();resetPositions();setupTextBoxes();setupObjects();setupCore();if(isIe)prepareFaces();importFramesFromJson();preparePlayBack();animateCubeBack(playFrames);
}
function resetPositions() {cube.cubelets.forEach(function(cubelet) {cubelet.css3DObject.position.copy(cubelet.position);cubelet.position.set(0, 0, 0);});
}
function setupObjects() {allObjects.push.apply(allObjects, cube.cubelets);allObjects.push.apply(allObjects, textBoxes);
}
function zeroPad(val, digits) {while (('' + val).length < digits) {val = '0' + val;}return val;
}
var dataStore = {'msgs': {'Certificate Moves': 'Moves','Certificate Time': 'Time','Certificate Title': 'Cube Solved','Directions 1': 'Swipe outside the cube to rotate it.','Directions 2': 'Swipe inside to twist the cube.','Directions 3': 'Keyboard commands work too.','Directions UI 1': 'Next','Directions UI 2': 'Done','Error Version 1': 'Oops! This doodle uses some experimental web ' +'technology that may not work on your device.','Error Version 2': 'To play, please use a supported hardware ' +'configuration and the latest version of one of these browsers:','Mobile Certificate Cube': 'Cube','Mobile Certificate Solved': 'Solved','Share Message': 'The Rubik’s Cube is 40! #CubeDoodle','Share Message Completed': '#CubeDoodle solved!!','Sign In Button': 'Sign in','Sign In Message': 'Sign in to add your name'},'origin': '','session': '','dir': '','hl': 'en','user': '','shortlink': 'google.com/doodles'
};
var parsedData = {};
var matches = window.location.hash.match(/[#&]data=([^&]*)/);
if (matches && matches.length > 1) {try {parsedData = JSON.parse(decodeURIComponent(matches[1])) || {};} catch (parseError) {}
}
mergeObject(parsedData, dataStore);
function mergeObject(from, to) {for (var key in from) {if (typeof from[key] == 'object') {mergeObject(from[key], to[key]);} else {to[key] = from[key] || to[key];}}
}
function setupTextBoxes() {if( isIe ){var ieScene        = new THREE.Object3D(),ieTextRenderer = new THREE.CSS3DRenderer(),ieCubeObject   = new THREE.Object3D(),ieAutoRotateObj= new THREE.Object3D(),container      = document.getElementById('container');container.appendChild( ieTextRenderer.domElement );ieTextRenderer.domElement.style.position = 'absolute';ieTextRenderer.domElement.style.top      = '0px';ieTextRenderer.domElement.style.left     = '0px';ieCubeObject.matrix      = cube.matrix;ieCubeObject.matrixWorld = cube.matrixWorld;ieCubeObject.matrixAutoUpdate = false;ieAutoRotateObj.matrix      = cube.autoRotateObj3D.matrix;ieAutoRotateObj.matrixWorld = cube.autoRotateObj3D.matrixWorld;ieAutoRotateObj.matrixAutoUpdate = false;ieCubeObject.name = 'cube'ieScene.add( ieAutoRotateObj );ieAutoRotateObj.add( ieCubeObject);function ieRender(){var containerWidth  = container.clientWidth,containerHeight = container.clientHeight;if( cube.domElement.parentNode &&( ieTextRenderer.domElement.clientWidth  !== containerWidth ||ieTextRenderer.domElement.clientHeight !== containerHeight )){ieTextRenderer.setSize( containerWidth, containerHeight );}ieTextRenderer.render( ieScene, cube.camera );requestAnimationFrame( ieRender );}requestAnimationFrame( ieRender );}var cubeObject = isIe ? {object3D: ieCubeObject }: cube;if(isMobile){certName = new ResizeableTextBox(cubeObject, 'certName', dataStore['msgs']['Mobile Certificate Cube']);certSolved = new ResizeableTextBox(cubeObject, 'certSolved', dataStore['msgs']['Mobile Certificate Solved']);}else{certSolved = new ResizeableTextBox(cubeObject, 'certSolved', dataStore['user']);certName = new ResizeableTextBox(cubeObject, 'certName',dataStore['msgs']['Certificate Title']);}var moves = cube.moveCounter;moves = zeroPad(moves, 4);certMovesNumber = new TextBox(cubeObject, 'certMovesNumber', moves);certMovesHeader = new TextBox(cubeObject, 'certMovesHeader',dataStore['msgs']['Certificate Moves']);var now = new Date();var month = ['JANUARY','FEBRUARY','MARCH','APRIL','MAY','JUNE','JULY','AUGUST','SEPTEMBER','OCTOBER','NOVEMBER','DECEMBER'][now.getMonth()];var dateString = now.getFullYear() + '-' +zeroPad(now.getMonth(), 2) + '-' +zeroPad(now.getDate(), 2);var lang = dataStore['hl'] || en;if (lang == 'en') {dateString = month + ' ' + now.getDate() + ', ' + now.getFullYear();} else if (now.toLocaleDateString) {dateString = now.toLocaleDateString(lang);}certDate = new TextBox(cubeObject, 'certDate', dateString);if (navigator.userAgent.indexOf('MSIE') > -1 ||navigator.userAgent.indexOf('Trident') > -1) {certDate.domElement.style.marginTop = '-210px';}var endTime = now.getTime();var elapsedTime = (endTime - startTime) / 1000;var timeSec = zeroPad(Math.floor(elapsedTime % 60), 2);var timeMin = zeroPad(Math.floor(elapsedTime / 60), 2);certTimeNumber =new TextBox(cubeObject, 'certTimeNumber', timeMin + ':' + timeSec);certTimeHeader = new TextBox(cubeObject, 'certTimeHeader',dataStore['msgs']['Certificate Time']);certLarryName = new TextBox(cubeObject, 'certLarryName', 'Lawrence Page');certErnoName = new TextBox(cubeObject, 'certErnoName', 'Erno Rubik');googleLogo = new DivBox(cubeObject, 'googleLogo');larrySignature = new DivBox(cubeObject, 'larrySignature');ernoSignature = new DivBox(cubeObject, 'ernoSignature');lineLeft = new DivBox(cubeObject, 'lineLeft');lineRight = new DivBox(cubeObject, 'lineRight');textBoxes = [certName, certSolved, certMovesNumber, certMovesHeader,certDate, certTimeNumber, certTimeHeader, certLarryName,certErnoName, googleLogo, larrySignature,ernoSignature, lineLeft, lineRight];textBoxes.forEach(function(textbox) {textbox.opacity = 0;textbox.css3DObject.element.style.opacity = 0;});
}
function setupCore() {var core = cube.cubelets[13];core.faces.forEach(function(face) {var logoFace = document.createElement('div');logoFace.classList.add('logoFace');face.element.appendChild(logoFace);});if (isIe) {var f = document.querySelectorAll(".faceDown .logoFace");f[0].classList.add("ie");}
}
function prepareFaces(){var f = document.querySelectorAll(".cube .face");for(var i=0;i<f.length;i++){f[i].style.webkitBackfaceVisibility = "visible";f[i].style.mozBackfaceVisibility = "visible";f[i].style.oBackfaceVisibility = "visible";f[i].style.backfaceVisibility = "visible";}
}
function preparePlayBack() {allObjects.forEach(function(object) {object.frames = [];});frames.forEach(function(frame, frameIndex) {frame.forEach(function(coords, objIndex) {var object = allObjects[coords.id];var tweenCoords = {x: coords.position.x,y: coords.position.y,z: coords.position.z,xr: coords.rotation.x,yr: coords.rotation.y,zr: coords.rotation.z,xrcss: coords.rotationCSS.x,yrcss: coords.rotationCSS.y,zrcss: coords.rotationCSS.z,opacity: coords.opacity};object.frames.push(tweenCoords);});});allObjects.forEach(function(object) {object.tweens = [];for (var i = 0; i < object.frames.length - 1; i++) {var frameTween = new TWEEN.Tween(object.frames[i]).to({x: object.frames[i + 1].x,y: object.frames[i + 1].y,z: object.frames[i + 1].z,xr: object.frames[i + 1].xr,yr: object.frames[i + 1].yr,zr: object.frames[i + 1].zr,xrcss: object.frames[i + 1].xrcss,yrcss: object.frames[i + 1].yrcss,zrcss: object.frames[i + 1].zrcss,opacity: object.frames[i + 1].opacity}, tweenSpeed).easing(TWEEN.Easing.Quartic.Out).onUpdate(function() {object.css3DObject.position.x = this.x;object.css3DObject.position.y = this.y;object.css3DObject.position.z = this.z;object.rotation.x = this.xr;object.rotation.y = this.yr;object.rotation.z = this.zr;object.css3DObject.rotation.x = this.xrcss;object.css3DObject.rotation.y = this.yrcss;object.css3DObject.rotation.z = this.zrcss;if (object.css3DObject.element) {object.css3DObject.element.style.opacity = this.opacity;} else {var i = 6;while (i-- > 0) {object.faces[i].element.style.opacity = this.opacity;}}object.opacity = this.opacity;});object.tweens.push(frameTween);}for (var i = 0; i < object.tweens.length - 1; i++) {object.tweens[i].chain(object.tweens[i + 1]);}});
}
function playFrames() {allObjects.forEach(function(object) {object.tweens[0].start(cube.time);});
}
function importFramesFromJson() {if (isMobile) {frames = certDataMobile.frames;} else {frames = certData.frames;}
}

源码地址 

点我下载icon-default.png?t=L9C2https://download.csdn.net/download/weixin_41937552/32536942

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/357167.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

15K的前端应届毕业生,就因为掌握了这些知识点!(前端企业级开发必备)

目录 1.前端开发 下载安装VScode 优化配置 2、插件安装 3、设置字体大小 4、开启完整的Emmet语法支持 5、视图 2.Node.js 入门 2.1、什么是Node.js 2.2、Node.js有什么用 2.3、安装 下载&#xff1a; 2.4、快速入门 2.5、服务器端应用开发 3、ES6入门 3.2、let声…

[Training Video - 1] [Selenium Basics] [Download and Install Selenium]

Download Selenium Jars Configure jars in eclipse Webdriver http://docs.seleniumhq.org/download/ Selenium RC http://code.google.com/p/selenium/downloads/detail?nameselenium-remote-control-1.0.3.zip&can2&q转载于:https://www.cnblogs.com/MasterMonkInT…

【0元试用微软 Azure人工智能认知服务】这个人脸识别功能太强大啦

导读&#xff1a;这个由美丽的客服小姐姐提前申请好了企业试用的账号&#xff0c;个人的话还得需要visa卡。 官网地址&#xff1a;azure.cn 登录地址&#xff1a;portal.azure.cn 目录 1.简介 2.Azure 认知服务是什么&#xff1f; ​ 3.认知服务的四种职能 4.人脸API 5.新建一…

OpenMap教程3 –使用MapHandler构建基本的地图应用程序–第2部分

1.简介 在上一教程中&#xff0c;我们看到了MapHandler如何将各种组件连接在一起&#xff08;更具体地说&#xff0c;是从MapHandlerChild派生的类&#xff09;。 我们看到了如何以编程方式执行此操作&#xff0c;但是还有另一种声明性地使用openmap.properties 。 样本OpenMa…

《javaScript100例|01》超级经典一套鼠标控制左右滚动图片带自动翻滚

目录 HTML示例 图片示例 HTML示例 <html > <head> <meta http-equiv"Content-Type" content"text/html; charsetgb2312" /> <title>超级经典一套鼠标控制左右滚动图片带自动翻滚</title> <style type"text/css&quo…

[BZOJ1007] [HNOI2008] 水平可见直线 (凸包)

Description 在xoy直角坐标平面上有n条直线L1,L2,...Ln,若在y值为正无穷大处往下看,能见到Li的某个子线段,则称Li为可见的,否则Li为被覆盖的. 例如,对于直线:L1:yx; L2:y-x; L3:y0 则L1和L2是可见的,L3是被覆盖的. 给出n条直线,表示成yAxB的形式(|A|,|B|<500000),且n条直线两…

《javaScript100例|02》超级经典一套鼠标控制左右滚动图片带自动翻滚

目录 效果图 JS示例 源码地址&#xff1a; 效果图 JS示例 <title>JavaScript切换图片</title> <script> function showDaTu(src){ document.getElementById("defaultImg").srcsrc; } </script> <img src"wall1.jpg" id"…

抽象工厂设计模式示例

本文是我们名为“ Java设计模式 ”的学院课程的一部分。 在本课程中&#xff0c;您将深入研究大量的设计模式&#xff0c;并了解如何在Java中实现和利用它们。 您将了解模式如此重要的原因&#xff0c;并了解何时以及如何应用模式中的每一个。 在这里查看 &#xff01; 目录 …

《javaScript100例|03》自写javaScript+CSS轮显效果

目录 效果图 代码示例 源码地址&#xff1a;自写JsCSS轮显效果.rar-互联网文档类资源-CSDN下载https://download.csdn.net/download/weixin_41937552/37380520 效果图 代码示例 <html xmlns"http://www.w3.org/1999/xhtml"> <head> <meta http-equ…

《javaScript100例|04》自动播放——Js幻灯片缓冲效果

目录 效果图 示例 源码地址&#xff1a; 效果图 示例 <html> <head> <title>自动播放——幻灯片缓冲效果</title> <style> body,div,ul,li{margin:0;padding:0;} ul{list-style-type:none;} body{background:#000;text-align:center;font:12p…

PHP+jquery 树状菜单

本项目开发过程中涉及树状菜单&#xff0c;于是做如下分享菜单实现 不足之处请大牛指点并见谅&#xff08;如图&#xff09; 1&#xff1a;数据表涉及字段 id int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 系统菜单ID, pid int(10) DEFAULT NULL COMMENT 父级ID, name va…

vue计算属性computed与监听属性watch的基本使用

目录 Vue.js 计算属性 Vue.js 监听属性 Vue.js 计算属性 计算属性关键词: computed。 计算属性在处理一些复杂逻辑时是很有用的。 反转字符串的示例 <!DOCTYPE html> <html> <he

vue样式绑定与事件处理器的基本使用

目录 Vue.js 样式绑定 Vue.js 事件处理器 事件修饰符 Vue.js 样式绑定

Finally 与 return

网上有很多人探讨Java中异常捕获机制try...catch...finally块中的finally语句是不是一定会被执行&#xff1f;很多人都说不是&#xff0c;当然他们的回答是正确的&#xff0c;经过我试验&#xff0c;至少有两种情况下finally语句是不会被执行的&#xff1a; &#xff08;1&…

vue表单的基本使用

Vue.js 表单 介绍 一下Vue.js 表单上的应用。你可以用 v-model 指令在表单控件元素上创建双向数据绑定。 v-model 会根据控件类型自动选取正确的方法来更新元素。 input 和 textarea 元素中使用 v-model 实现双向数据绑定: <!DOCTYPE html> <html> <head…

fold函数_Java中使用Map and Fold进行函数式编程

fold函数在函数式编程中&#xff0c;Map和Fold是两个非常有用的运算符&#xff0c;它们属于每种函数式语言。 如果Map和Fold运算符是如此强大且必不可少&#xff0c;那么您如何解释说即使Java编程语言缺少这两个运算符&#xff0c;我们也可以使用Java来完成工作&#xff1f; 事…

NVIC优先级分组

挂起&#xff0c;解挂&#xff0c;使能&#xff0c;失能 转载于:https://www.cnblogs.com/alantechnique/p/5598374.html

【Egret】Wing3发布移动APP功能,打包APK流程以及会遇到的问题

流程&#xff1a;1.安装好wing 3&#xff0c;然后 插件——Egret项目工具——发布移动APP——Build android native project 2.配置发布环境&#xff1a; Android 1.egret engine 点击下载2.android sdk mac版下载 windows版下载3.java sdk mac版下载 windows版…

有效运维的 on-call 机制

[编者按]本文作者为云告警平台OneAlert负责人&#xff0c;著《云计算与OpenStack》&#xff0c;在IT运营管理、云计算方面从业10多年。 正文 互联网技术的发展&#xff0c;离不开运维支撑工作&#xff0c;没有零bug的程序&#xff0c;没有不出问题的系统&#xff0c;问题故障不…

团队项目:界面设计

游戏内关卡为2D横版界面&#xff0c;背景为楼群贴图&#xff0c;整体色调以灰色为主&#xff0c;与摩托车手的主题相配。游戏中配有音效及背景音乐&#xff0c;背景音乐为我组成员自行创作录制完成。界面展示如下&#xff1a; 图1 游戏中设计图 图中显示的摩托车手即为玩家操控…