本文主要是介绍three.js使用超炫酷的鼠标跟随案例,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
three.js使用超炫酷的鼠标跟随案例
文章目录
- three.js使用超炫酷的鼠标跟随案例
- 前言
- 一、效果展示
- 二、直接上代码
- 1.效果代码
- 2.注意点
- 总结
前言
three.js在浏览器中就能够简单和轻便渲染出3D场景.
一、效果展示
二、直接上代码
1.效果代码
<!DOCTYPE html>
<meta charset="utf-8">
<style>body {margin: 0;background: #111;min-width: 960px;
}</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>var width = Math.max(960, innerWidth),height = Math.max(500, innerHeight);var x1 = width / 2,y1 = height / 2,x0 = x1,y0 = y1,i = 0,r = 200,τ = 2 * Math.PI;var canvas = d3.select("body").append("canvas").attr("width", width).attr("height", height).on("ontouchstart" in document ? "touchmove" : "mousemove", move);var context = canvas.node().getContext("2d");
context.globalCompositeOperation = "lighter";
context.lineWidth = 2;d3.timer(function() {context.clearRect(0, 0, width, height);var z = d3.hsl(++i % 360, 1, .5).rgb(),c = "rgba(" + z.r + "," + z.g + "," + z.b + ",",x = x0 += (x1 - x0) * .1,y = y0 += (y1 - y0) * .1;d3.select({}).transition().duration(2000).ease(Math.sqrt).tween("circle", function() {return function(t) {context.strokeStyle = c + (1 - t) + ")";context.beginPath();context.arc(x, y, r * t, 0, τ);context.stroke();};});
});function move() {var mouse = d3.mouse(this);x1 = mouse[0];y1 = mouse[1];d3.event.preventDefault();
}</script>
2.注意点
需要引入d3文件
<script src="//d3js.org/d3.v3.min.js"></script>
总结
参考文章:
https://blog.csdn.net/qq_26822029/article/details/91353209
本篇文章的思路很好
这篇关于three.js使用超炫酷的鼠标跟随案例的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!