Web前端笔记-two.js图形旋转动画的2种实现方式
生活随笔
收集整理的這篇文章主要介紹了
Web前端笔记-two.js图形旋转动画的2种实现方式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這里有兩種方式!
第一種是使用setInterval:
代碼如下:
let time = setInterval(function(){if(sun.sun.rotation >= TWO_PI - 0.0625){sun.sun.rotation = 0;}sun.sun.rotation += (TWO_PI - sun.sun.rotation) * 0.0625;}, 300);運行截圖如下:
?
?
?
第二種是使用two.bind('update', function(frameCount){})
代碼如下:
two.bind('update', function (frameCount){if(sun.sun.rotation >= TWO_PI - 0.0625){sun.sun.rotation = 0;console.log("Hello");}sun.sun.rotation += (TWO_PI - sun.sun.rotation) * 0.0625;}).play();運行截圖如下:
第2種方式效果比第1種方式要好。
項目代碼draw.js
import {SunElement} from "JS/elements/sun";;import * as Two from "JS/two"; import * as $ from "JS/jquery";let two; let mouse; let isPressed = false; let originalPositionX = 0; let originalPositionY = 0; let map = new Map(); let rect; let TWO_PI = Math.PI * 2; let sunArry;export function drawGraphic(){let elem = document.getElementById("draw-shapes");let params = {type: Two.Types['webgl'],fullscreen: true,autostart: true};two = new Two(params).appendTo(elem);mouse = new Two.ZUI(two.scene);mouse.addLimits(0.1, 10);let $stage = $(two.renderer.domElement);$stage.bind('mousewheel wheel', function(event){let e = event.originalEvent;e.stopPropagation();e.preventDefault();let dy = (e.wheelDeltaY || -e.deltaY) / 1000;mouse.zoomBy(dy, e.clientX, e.clientY);});$stage.bind('mouseup', function(event){isPressed = false;});$stage.bind('mouseout', function(event){isPressed = false;});$stage.bind('mousedown', function(event){isPressed = true;originalPositionX = event.clientX;originalPositionY = event.clientY;let x = event.clientX;let y = event.clientY;for(let value of map){let xOffset = value[0]._width / 2;let yOffset = value[0]._height / 2;let letX = ((value[0]._translation._x - xOffset) * (two.scene._matrix.elements[0]) + two.scene._matrix.elements[2]);let letY = ((value[0]._translation._y - yOffset) * (two.scene._matrix.elements[4]) + two.scene._matrix.elements[5]);let letWidth = value[0]._width * two.scene._matrix.elements[0];let letHeight = value[0]._height * two.scene._matrix.elements[4];if(x > letX &&y > letY &&x < letX + letWidth &&y < letY + letHeight){let r = Math.round(Math.random() * 255);let g = Math.round(Math.random() * 255);let b = Math.round(Math.random() * 255);let rgbStr = "rgb(" + r + "," + g + "," + b + ")";value[0].fill = rgbStr;break;}}});$stage.bind('mousemove', function(event){if(isPressed){let boolX = event.clientX - originalPositionX;let boolY = event.clientY - originalPositionY;// console.log(boolX + " " + boolY);mouse.graphicMove(boolX, boolY);originalPositionX = event.clientX;originalPositionY = event.clientY;}});createBtn(1001, 200, 200, 500, "red");createBtn(1002, 400, 400, 500, "green");createBtn(1003, 600, 600, 500, "blue");createBtn(1004, 800, 800, 500, "black");createBtn(1005, 1000, 1000, 500, "yellow");createBtn(1006, 400, 800, 500, "purple");let value = {two: two,x: 800,y: 200,len: 100};let sun = new SunElement(value);two.bind('update', function (frameCount){if(sun.sun.rotation >= TWO_PI - 0.0625){sun.sun.rotation = 0;console.log("Hello");}sun.sun.rotation += (TWO_PI - sun.sun.rotation) * 0.0625;}).play();// let time = setInterval(function(){//// if(sun.sun.rotation >= TWO_PI - 0.0625){//// sun.sun.rotation = 0;// }// sun.sun.rotation += (TWO_PI - sun.sun.rotation) * 0.0625;//// }, 300);}function createBtn(id, x, y, weight, color) {rect = two.makeRectangle(x, y, 200, 200);rect.noStroke();rect.fill = color;rect.myId = id;map.set(rect, weight); }//計算當前屏幕圓心 對應的 圖形坐標 function getScreenOriginal(){let original = {x: 0,y: 0};original.x = two.width / 2;original.y = two.height / 2;// console.log(two.scene._matrix.elements)//獲取水平位移及垂直位移//將瀏覽器上界面坐標轉換為two.js的場景坐標,也就是 cirX和cirY為當前界面中點的場景坐標let cirX = (original.x - two.scene._matrix.elements[2]) / two.scene._matrix.elements[0];let cirY = (original.y - two.scene._matrix.elements[5]) / two.scene._matrix.elements[4];console.log("當前圓心 cirX:" + cirX + " cirY:" + cirY);original.x = cirX;original.y = cirY;return original; }export function flyToPosition(x, y){//當前屏幕中點 對應的 場景坐標let dot = getScreenOriginal();// console.log(dot);let c = two.makeCircle(x, y, 10);c.fill = "red";let differenceValueX = (dot.x - x) * two.scene._matrix.elements[0];let differenceValueY = (dot.y - y) * two.scene._matrix.elements[4];console.log(two.scene._matrix.elements);console.log("差值:"+ differenceValueX + " " + differenceValueY);//飛到對應x,y坐標點,這個x, y將會變成新的屏幕中心點mouse.graphicMove(differenceValueX, differenceValueY);originalPositionX = differenceValueX;originalPositionY = differenceValueY;}圖中那個太陽相關代碼:
;export class SunElement{constructor({two, x, y, len}){this.x = x;this.y = y;this.two = two;this.len = len;this.render();}render() {let color = "rgba(255, 128, 0, 0.66)";let sun = this.two.makeGroup();let radius = this.len;let gutter = this.len / 5;let core = this.two.makeCircle(this.x, this.y, radius);core.noStroke();core.fill = color;sun.core = core;let coronas = [];let corona_amount = 16;for(let i = 0; i < corona_amount; i++){let pct = (i + 1) / corona_amount;let theta = pct * Math.PI * 2;let x = (radius + gutter) * Math.cos(theta);let y = (radius + gutter) * Math.sin(theta);console.log("x:" + x + " Math.cos(theta):" + Math.cos(theta) + " theta:" + theta);let corona = this.makeTriangle(gutter);corona.noStroke();corona.fill = color;corona.translation.set(x, y);corona.rotation = Math.atan2(-y , -x) + Math.PI / 2;coronas.push(corona);}sun.add(coronas);this.sun = sun;sun.translation.set(this.x, this.y)return sun;}makeTriangle(size){let tri = this.two.makePath(-size / 2, 0, size / 2, 0, 0, size);return tri;} }?
?
總結
以上是生活随笔為你收集整理的Web前端笔记-two.js图形旋转动画的2种实现方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python笔记-Can’t recon
- 下一篇: C++笔记-二维棋盘数组使用BFS(宽度