热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

小猫抓毛线球特效

小猫抓毛线球特效效果图:代码如下,复制即可使用:<!DOCTYPEhtml><html><head><metac

小猫抓毛线球特效

 效果图:

 

代码如下,复制即可使用:

DOCTYPE html>
<html >
<head>
  <meta charset="UTF-8">
  <title>Cat vs ball of wooltitle>
  
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css">
<style>
    @import url(http://fonts.googleapis.com/css?family=Open+Sans:800);

#world{
    position: absolute;
    width:100%;
    height: 100%;
    background-color: #6ecccc;
  overflow:hidden;
}

#credits{
  position:absolute;
  width:100%;
  margin: auto;
  bottom:0;
  margin-bottom:20px;
  font-family:'Open Sans', sans-serif;
  color:#328685;
  font-size:0.7em;
  text-transform: uppercase;
  text-align : center;
}
#credits a {
  color:#328685;
}

#credits a:hover {
  color:#630d15;
}
style>


  
head>

<body>
      <div id="world"/>
  <script src='http://cdnjs.cloudflare.com/ajax/libs/three.js/r70/three.min.js'>script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/gsap/1.16.1/TweenMax.min.js'>script>
<script src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/264161/OrbitControls.js'>script>
<script src='https://s3-us-west-2.amazonaws.com/s.cdpn.io/264161/Cat.js'>script>

    <script>
        
//THREEJS RELATED VARIABLES 

var scene,
    camera, fieldOfView, aspectRatio, nearPlane, farPlane,
    gobalLight, shadowLight, backLight,
    renderer,
    container,
    controls;

//SCREEN & MOUSE VARIABLES

var HEIGHT, WIDTH, windowHalfX, windowHalfY,
    mousePos = { x: 0, y: 0 },
    oldMousePos = {x:0, y:0},
    ballWallDepth = 28;


//3D OBJECTS VARIABLES

var hero;

//INIT THREE JS, SCREEN AND MOUSE EVENTS

function initScreenAnd3D() {
  
  HEIGHT = window.innerHeight;
  WIDTH = window.innerWidth;
  windowHalfX = WIDTH / 2;
  windowHalfY = HEIGHT / 2;

  scene = new THREE.Scene();
  aspectRatio = WIDTH / HEIGHT;
  fieldOfView = 50;
  nearPlane = 1;
  farPlane = 2000;
  camera = new THREE.PerspectiveCamera(
    fieldOfView,
    aspectRatio,
    nearPlane,
    farPlane
    );
  camera.position.x = 0;
  camera.position.z = 300;
  camera.position.y = 250;
  camera.lookAt(new THREE.Vector3(0, 60, 0));

  renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true });
  renderer.setSize(WIDTH, HEIGHT);
  renderer.shadowMapEnabled = true;
  
  container = document.getElementById('world');
  container.appendChild(renderer.domElement);
  
  window.addEventListener('resize', handleWindowResize, false);
  document.addEventListener('mousemove', handleMouseMove, false);
  document.addEventListener('touchmove', handleTouchMove, false);
  
  /*
  cOntrols= new THREE.OrbitControls(camera, renderer.domElement);
  controls.minPolarAngle = -Math.PI / 2; 
  controls.maxPolarAngle = Math.PI / 2;
  controls.noZoom = true;
  controls.noPan = true;
  //*/


}

function handleWindowResize() {
  HEIGHT = window.innerHeight;
  WIDTH = window.innerWidth;
  windowHalfX = WIDTH / 2;
  windowHalfY = HEIGHT / 2;
  renderer.setSize(WIDTH, HEIGHT);
  camera.aspect = WIDTH / HEIGHT;
  camera.updateProjectionMatrix();
}

function handleMouseMove(event) {
  mousePos = {x:event.clientX, y:event.clientY};
} 

function handleTouchMove(event) {
  if (event.touches.length == 1) {
    event.preventDefault();
    mousePos = {x:event.touches[0].pageX, y:event.touches[0].pageY};
  }
}

function createLights() {
  globalLight = new THREE.HemisphereLight(0xffffff, 0xffffff, .5)
  
  shadowLight = new THREE.DirectionalLight(0xffffff, .9);
  shadowLight.position.set(200, 200, 200);
  shadowLight.castShadow = true;
  shadowLight.shadowDarkness = .2;
  shadowLight.shadowMapWidth = shadowLight.shadowMapHeight = 2048;
  
  backLight = new THREE.DirectionalLight(0xffffff, .4);
  backLight.position.set(-100, 100, 100);
  backLight.castShadow = true;
  backLight.shadowDarkness = .1;
  backLight.shadowMapWidth = shadowLight.shadowMapHeight = 2048;
  
  scene.add(globalLight);
  scene.add(shadowLight);
  scene.add(backLight);
}

function createFloor(){ 
  floor = new THREE.Mesh(new THREE.PlaneBufferGeometry(1000,1000), new THREE.MeshBasicMaterial({color: 0x6ecccc}));
  floor.rotation.x = -Math.PI/2;
  floor.position.y = 0;
  floor.receiveShadow = true;
  scene.add(floor);
}

function createHero() {
  hero = new Cat();
  scene.add(hero.threeGroup);
}

function createBall() {
  ball = new Ball();
  scene.add(ball.threeGroup);
}

// BALL RELATED CODE


var woolNodes = 10,
    woolSegLength = 2,
    gravity = -.8,
    accuracy =1;


Ball = function(){

    var redMat = new THREE.MeshLambertMaterial ({
        color: 0x630d15, 
        shading:THREE.FlatShading
    });

    var stringMat = new THREE.LineBasicMaterial({
        color: 0x630d15,
        linewidth: 3
    });

    this.threeGroup = new THREE.Group();
    this.ballRay = 8;

    this.verts = [];

    // string
    var stringGeom = new THREE.Geometry();

    for (var i=0; i< woolNodes; i++    ){
        var v = new THREE.Vector3(0, -i*woolSegLength, 0);
        stringGeom.vertices.push(v);

        var woolV = new WoolVert();
        woolV.x = woolV.oldx = v.x;
        woolV.y = woolV.oldy = v.y;
        woolV.z = 0;
        woolV.fx = woolV.fy = 0;
        woolV.isRootNode = (i==0);
        woolV.vertex = v;
        if (i > 0) woolV.attach(this.verts[(i - 1)]);
        this.verts.push(woolV);
        
    }
      this.string = new THREE.Line(stringGeom, stringMat);

      // body
      var bodyGeom = new THREE.SphereGeometry(this.ballRay, 5,4);
    this.body = new THREE.Mesh(bodyGeom, redMat);
      this.body.position.y = -woolSegLength*woolNodes;

      var wireGeom = new THREE.TorusGeometry( this.ballRay, .5, 3, 10, Math.PI*2 );
      this.wire1 = new THREE.Mesh(wireGeom, redMat);
      this.wire1.position.x = 1;
      this.wire1.rotation.x = -Math.PI/4;

      this.wire2 = this.wire1.clone();
      this.wire2.position.y = 1;
      this.wire2.position.x = -1;
      this.wire1.rotation.x = -Math.PI/4 + .5;
      this.wire1.rotation.y = -Math.PI/6;

      this.wire3 = this.wire1.clone();
      this.wire3.rotation.x = -Math.PI/2 + .3;

      this.wire4 = this.wire1.clone();
      this.wire4.position.x = -1;
      this.wire4.rotation.x = -Math.PI/2 + .7;

      this.wire5 = this.wire1.clone();
      this.wire5.position.x = 2;
      this.wire5.rotation.x = -Math.PI/2 + 1;

      this.wire6 = this.wire1.clone();
      this.wire6.position.x = 2;
      this.wire6.position.z = 1;
      this.wire6.rotation.x = 1;

      this.wire7 = this.wire1.clone();
      this.wire7.position.x = 1.5;
      this.wire7.rotation.x = 1.1;

      this.wire8 = this.wire1.clone();
      this.wire8.position.x = 1;
      this.wire8.rotation.x = 1.3;

      this.wire9 = this.wire1.clone();
      this.wire9.scale.set(1.2,1.1,1.1);
      this.wire9.rotation.z = Math.PI/2;
      this.wire9.rotation.y = Math.PI/2;
      this.wire9.position.y = 1;
      
      this.body.add(this.wire1);
      this.body.add(this.wire2);
      this.body.add(this.wire3);
      this.body.add(this.wire4);
      this.body.add(this.wire5);
      this.body.add(this.wire6);
      this.body.add(this.wire7);
      this.body.add(this.wire8);
      this.body.add(this.wire9);

      this.threeGroup.add(this.string);
    this.threeGroup.add(this.body);

    this.threeGroup.traverse( function ( object ) {
    if ( object instanceof THREE.Mesh ) {
      object.castShadow = true;
      object.receiveShadow = true;
    }});

}

/* 
The next part of the code is largely inspired by this codepen :
http://codepen.io/dissimulate/pen/KrAwx?editors=001
thanks to dissimulate for his great work
*/

/*
Copyright (c) 2013 dissimulate at Codepen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/



WoolVert = function(){
    this.x = 0;
    this.y = 0;
    this.z = 0;
    this.oldx = 0;
    this.oldy = 0;
    this.fx = 0;
    this.fy = 0;
    this.isRootNode = false;
    this.constraints = [];
    this.vertex = null;
}


WoolVert.prototype.update = function(){
    var wind = 0;//.1+Math.random()*.5;
      this.add_force(wind, gravity);

      nx = this.x + ((this.x - this.oldx)*.9) + this.fx;
      ny = this.y + ((this.y - this.oldy)*.9) + this.fy;
      this.oldx = this.x;
      this.oldy = this.y;
      this.x = nx;
      this.y = ny;

      this.vertex.x = this.x;
      this.vertex.y = this.y;
      this.vertex.z = this.z;

      this.fy = this.fx = 0
}

WoolVert.prototype.attach = function(point) {
  this.constraints.push(new Constraint(this, point));
};

WoolVert.prototype.add_force = function(x, y) {
  this.fx += x;
  this.fy += y;
};

Constraint = function(p1, p2) {
  this.p1 = p1;
  this.p2 = p2;
  this.length = woolSegLength;
};

Ball.prototype.update = function(posX, posY, posZ){
        
    var i = accuracy;
    
    while (i--) {
        
        var nodesCount = woolNodes;
        
        while (nodesCount--) {
        
            var v = this.verts[nodesCount];
            
            if (v.isRootNode) {
                v.x = posX;
                v.y = posY;
                v.z = posZ;
            }
        
            else {
        
                var constraintsCount = v.constraints.length;
                  
                  while (constraintsCount--) {
                      
                      var c = v.constraints[constraintsCount];

                      var diff_x = c.p1.x - c.p2.x,
                        diff_y = c.p1.y - c.p2.y,
                        dist = Math.sqrt(diff_x * diff_x + diff_y * diff_y),
                        diff = (c.length - dist) / dist;

                      var px = diff_x * diff * .5;
                      var py = diff_y * diff * .5;

                      c.p1.x += px;
                      c.p1.y += py;
                      c.p2.x -= px;
                      c.p2.y -= py;
                      c.p1.z = c.p2.z = posZ;
                  }

                  if (nodesCount == woolNodes-1){
                      this.body.position.x = this.verts[nodesCount].x;
                    this.body.position.y = this.verts[nodesCount].y;
                    this.body.position.z = this.verts[nodesCount].z;

                    this.body.rotation.z += (v.y <= this.ballRay)? (v.oldx-v.x)/10 : Math.min(Math.max( diff_x/2, -.1 ), .1);
                  }
              }
              
              if (v.y < this.ballRay) {
                  v.y = this.ballRay;
              }
        }
    }

    nodesCount = woolNodes;
    while (nodesCount--) this.verts[nodesCount].update();

    this.string.geometry.verticesNeedUpdate = true;

    
}

Ball.prototype.receivePower = function(tp){
    this.verts[woolNodes-1].add_force(tp.x, tp.y);
}

// Enf of the code inspired by dissmulate


// Make everything work together :

var t=0;

function loop(){
  render();
  
  t+=.05;
  hero.updateTail(t);

  var ballPos = getBallPos();
  ball.update(ballPos.x,ballPos.y, ballPos.z);
  ball.receivePower(hero.transferPower);
  hero.interactWithBall(ball.body.position);

  requestAnimationFrame(loop);
}


function getBallPos(){
  var vector = new THREE.Vector3();

  vector.set(
      ( mousePos.x / window.innerWidth ) * 2 - 1, 
      - ( mousePos.y / window.innerHeight ) * 2 + 1,
      0.1 );

  vector.unproject( camera );
  var dir = vector.sub( camera.position ).normalize();
  var distance = (ballWallDepth - camera.position.z) / dir.z;
  var pos = camera.position.clone().add( dir.multiplyScalar( distance ) );
  return pos;
}

function render(){
  if (controls) controls.update();
  renderer.render(scene, camera);
}

window.addEventListener('load', init, false);

function init(event){
  initScreenAnd3D();
  createLights();
  createFloor()
  createHero();
  createBall();
  loop();
}
    script>

body>
html>

 如有错误,欢迎联系我改正,非常感谢!!!


推荐阅读
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文介绍了使用Java实现大数乘法的分治算法,包括输入数据的处理、普通大数乘法的结果和Karatsuba大数乘法的结果。通过改变long类型可以适应不同范围的大数乘法计算。 ... [详细]
  • javascript  – 概述在Firefox上无法正常工作
    我试图提出一些自定义大纲,以达到一些Web可访问性建议.但我不能用Firefox制作.这就是它在Chrome上的外观:而那个图标实际上是一个锚点.在Firefox上,它只概述了整个 ... [详细]
  • 本文详细解析了JavaScript中相称性推断的知识点,包括严厉相称和宽松相称的区别,以及范例转换的规则。针对不同类型的范例值,如差别范例值、统一类的原始范例值和统一类的复合范例值,都给出了具体的比较方法。对于宽松相称的情况,也解释了原始范例值和对象之间的比较规则。通过本文的学习,读者可以更好地理解JavaScript中相称性推断的概念和应用。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • 如何使用Java获取服务器硬件信息和磁盘负载率
    本文介绍了使用Java编程语言获取服务器硬件信息和磁盘负载率的方法。首先在远程服务器上搭建一个支持服务端语言的HTTP服务,并获取服务器的磁盘信息,并将结果输出。然后在本地使用JS编写一个AJAX脚本,远程请求服务端的程序,得到结果并展示给用户。其中还介绍了如何提取硬盘序列号的方法。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • Windows下配置PHP5.6的方法及注意事项
    本文介绍了在Windows系统下配置PHP5.6的步骤及注意事项,包括下载PHP5.6、解压并配置IIS、添加模块映射、测试等。同时提供了一些常见问题的解决方法,如下载缺失的msvcr110.dll文件等。通过本文的指导,读者可以轻松地在Windows系统下配置PHP5.6,并解决一些常见的配置问题。 ... [详细]
  • 本文讨论了为什么在main.js中写import不会全局生效的问题,并提供了解决方案。在每一个vue文件中都需要写import语句才能使其生效,而在main.js中写import语句则不会全局生效。本文还介绍了使用Swal和sweetalert2库的示例。 ... [详细]
  • 本文介绍了OC学习笔记中的@property和@synthesize,包括属性的定义和合成的使用方法。通过示例代码详细讲解了@property和@synthesize的作用和用法。 ... [详细]
  • Mac OS 升级到11.2.2 Eclipse打不开了,报错Failed to create the Java Virtual Machine
    本文介绍了在Mac OS升级到11.2.2版本后,使用Eclipse打开时出现报错Failed to create the Java Virtual Machine的问题,并提供了解决方法。 ... [详细]
  • 生成对抗式网络GAN及其衍生CGAN、DCGAN、WGAN、LSGAN、BEGAN介绍
    一、GAN原理介绍学习GAN的第一篇论文当然由是IanGoodfellow于2014年发表的GenerativeAdversarialNetworks(论文下载链接arxiv:[h ... [详细]
  • baresip android编译、运行教程1语音通话
    本文介绍了如何在安卓平台上编译和运行baresip android,包括下载相关的sdk和ndk,修改ndk路径和输出目录,以及创建一个c++的安卓工程并将目录考到cpp下。详细步骤可参考给出的链接和文档。 ... [详细]
  • t-io 2.0.0发布-法网天眼第一版的回顾和更新说明
    本文回顾了t-io 1.x版本的工程结构和性能数据,并介绍了t-io在码云上的成绩和用户反馈。同时,还提到了@openSeLi同学发布的t-io 30W长连接并发压力测试报告。最后,详细介绍了t-io 2.0.0版本的更新内容,包括更简洁的使用方式和内置的httpsession功能。 ... [详细]
  • 知识图谱——机器大脑中的知识库
    本文介绍了知识图谱在机器大脑中的应用,以及搜索引擎在知识图谱方面的发展。以谷歌知识图谱为例,说明了知识图谱的智能化特点。通过搜索引擎用户可以获取更加智能化的答案,如搜索关键词"Marie Curie",会得到居里夫人的详细信息以及与之相关的历史人物。知识图谱的出现引起了搜索引擎行业的变革,不仅美国的微软必应,中国的百度、搜狗等搜索引擎公司也纷纷推出了自己的知识图谱。 ... [详细]
author-avatar
9527
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有