Mouse Mobile Fireworks

Source: Internet
Author: User

<! DOCTYPE html>

<style class= "Cp-pen-styles" >body {

Background: #333;

padding:0;

margin:0;

width:100%;

height:100%;

Overflow:hidden;

}

. Datgui {

Position:absolute;

top:0;

z-index:100;

right:0;

opacity:0.3;

Transition:opacity 700ms ease-out;

}

. datgui:hover {

Opacity:1;

}

</style>

<div class= "Datgui" ></div>

<script src= '//production-assets.codepen.io/assets/common/ Stopexecutionontimeout-58d22c749295bca52f487966e382a94a495ac103faca9206cbd160bdf8aedf2a.js ' ></script> <script src= '//cdnjs.cloudflare.com/ajax/libs/dat-gui/0.6.0/dat.gui.min.js ' ></script>

<script> ' use strict ';

var _class2, _temp, _initialiseprops;

function _classcallcheck (instance, Constructor) {if (!) ( Instance instanceof Constructor) {throw new TypeError ("Cannot call a class as a function");}}

Blob Class

Generic particle class for both grow and explode

var blob = function blob (config) {

var _this = this;

_classcallcheck (this, Blob);

This.update = function () {

_this.x + = _THIS.VX;

_this.y + = _this.vy;

if (_this.diretion = = = ' Up ' && _this.size.start > _this.size.end-1 | | _this.diretion = = = ' Down ' && _t His.size.start < _this.size.end + 1) {

_this.died = true;

}

_this.size.start = _this.size.start-(_this.size.start-_this.size.end) * 0.1;

};

This.x = config.x;

This.y = CONFIG.Y;

THIS.VX = CONFIG.VX;

This.vy = Config.vy;

This.origin = {

X:config.origin.x,

Y:config.origin.y

};

This.size = {

Start:config.size.start,

End:config.size.end

};

This.diretion = This.size.start < this.size.end? ' Up ': ' Down ';

This.operation = config.operation;

this.died = config.died;

This.color = Config.color;

};

Render Class//

var Render = (_temp = _class2 = function () {

function Render (Element) {

_classcallcheck (this, Render);

_initialiseprops.call (this);

This.element = element;

This.operation = ' xor ';

This.canvas = This.createcanvas (' canvas ');

This.points = [];

This.tick = 0;

This.ops = ~ ~ (Math.random () * (this.operation.length-1));

Window.addeventlistener (' resize ', This.resetcanvas);

Window.addeventlistener (' MouseMove ', this.createpoint);

This.renderloop ();

}

Set Options from GUI

Render.prototype.drawLine = function DrawLine (point1, Point2) {

This.surface.beginPath ();

This.surface.strokeStyle = Point1.color;

This.surface.lineWidth = 0.1;

This.surface.moveTo (point1.x, POINT1.Y);

This.surface.lineTo (point2.x, POINT2.Y);

This.surface.stroke ();

};

return Render;

} (), _initialiseprops = function _initialiseprops () {

var _this2 = this;

this.setoptions = function (options) {

_this2.operation = options.operation;

_this2.basehue = Options.basehue;

_this2.basesize = options.basesize;

_this2.basespeed = Options.basespeed;

_this2.drawblobs = Options.drawblobs;

_this2.drawlines = Options.drawlines;

_this2.motion = options.motion;

_this2.speed = Options.speed;

};

This.setviewport = function (Element) {

var canvaselement = element;

var width = ~ ~ (document.documentElement.clientWidth, window.innerwidth | | 0);

var height = ~ ~ (document.documentElement.clientHeight, Window.innerheight | | 0);

_this2.width = width;

_this2.height = height;

Canvaselement.width = _this2.width;

Canvaselement.height = _this2.height;

};

This.createcanvas = function (name) {

var canvaselement = document.createelement (' canvas ');

Canvaselement.id = name;

_this2.setviewport (canvaselement);

_this2.element.appendchild (canvaselement);

_this2.surface = Canvaselement.getcontext (' 2d ');

_this2.surface.scale (1, 1);

return canvaselement;

};

This.resetcanvas = function () {

Window.cancelanimationframe (_this2.animation);

_this2.setviewport (_this2.canvas);

_this2.renderloop ();

};

This.createpoint = function (event, demo) {

_this2.tick++;

if (_this2.tick > 2) {

_this2.tick = 0;

var x = event? Event.pagex: _THIS2.WIDTH/2;

var y = event? Event.pagey: _THIS2.HEIGHT/2;

var end = _this2.basesize + ~ ~ (math.random () * 30);

var start = demo? end/1.1:0;

var origin = {

X:x,

Y:y

};

var point = new Blob ({

X:x,

Y:y,

Vx:Math.random () * _this2.basespeed-_THIS2.BASESPEED/2,

Vy:Math.random () * _this2.basespeed-_THIS2.BASESPEED/2,

Origin:origin,

Size: {

Start:start,

End:end

},

Died:false,

Color: ' HSL (' + (_this2.basehue + ~ ~ (math.random () * 60) + ', 100%, 50%) '

});

_this2.points.push (point);

}

};

This.createpop = function (config) {

var angle = Math.random () * 2 * MATH.PI;

var radius = math.random () * Config.size.start * CONFIG.SIZE.START;

var pointx = config.x + math.sqrt (RADIUS) * Math.Cos (angle);

var pointy = config.y + math.sqrt (RADIUS) * Math.sin (angle);

var point = new Blob ({

X:pointx,

Y:pointy,

VX: (pointx-config.x) * 0.1 + CONFIG.VX,

VY: (POINTY-CONFIG.Y) * 0.1 + Config.vy,

Origin: {

X:config.x,

Y:config.y

},

Size: {

START:CONFIG.SIZE.START/2,

end:0

},

Died:false,

Color:config.color

});

_this2.points.push (point);

};

This.draw = function (point) {

_this2.surface.beginpath ();

_this2.surface.fillstyle = Point.color;

_this2.surface.arc (Point.x, Point.y, Point.size.start, 0, 2 * Math.PI, FALSE);

_this2.surface.fill ();

};

This.renderloop = function () {

_this2.surface.globalcompositeoperation = _this2.operation;

if (_this2.motion) {

_this2.surface.drawimage (_this2.canvas, 0,-_this2.speed);

}

_this2.surface.fillstyle = ' Rgba (0,0,0,0.08) ';

_this2.surface.fillrect (0, 0, _this2.width, _this2.height);

for (var x = 0; x < _this2.points.length; × x + +) {

var point = _this2.points[x];

if (_this2.drawblobs) {

_this2.draw (point);

}

Point.update ();

if (x < _this2.points.length-1 && _this2.drawlines) {

_this2.drawline (Point, Point.origin);

}

if (point.died) {

if (point.diretion = = = ' Up ') {

_this2.points.splice (x, 1);

var Amt = ~ ~ (math.random () * 10) + 10;

for (var i = 0; i < Amt; i++) {

_this2.createpop (point);

}

} else {

_this2.points.splice (x, 1);

}

}

}

_this2.animation = Window.requestanimationframe (_this2.renderloop);

};

}, _temp);

GUI Functions and Options

var assignoptions = function assignoptions () {

Demo.setoptions (options);

};

var options = {

BASEHUE:40,

Basesize:10,

Basespeed:5,

Operation: ' Source-over ',

Drawblobs:true,

Drawlines:false,

Motion:false,

Speed:3

};

var demo = new Render (document.body);

Assignoptions ();

var event = {

PageX: ~ ~ (document.documentElement.clientWidth, window.innerwidth | | 0)/2,

Pagey: ~ ~ (document.documentElement.clientHeight, Window.innerheight | | 0)/2

};

for (var i = 0; i < i++) {

Demo.createpoint (event, {

Start:true

});

}

GUI Folders

var gui = new dat. GUI ();

var folderparticle = gui.addfolder (' particle Options ');

var folderrender = Gui.addfolder (' Render Options ');

var foldermotion = Gui.addfolder (' Add Motion ');

Folderparticle.add (Options, ' basesize ', 1, +). Step (1). Onfinishchange (assignoptions);

Folderrender.add (Options, ' Basehue ', 1, ()). Step (1). Onfinishchange (assignoptions);

Folderparticle.add (Options, ' Basespeed ', 1). Step (1). Onfinishchange (assignoptions);

Folderrender.add (Options, ' drawblobs '). Onfinishchange (assignoptions);

Folderrender.add (Options, ' DrawLines '). Onfinishchange (assignoptions);

Folderrender.add (options, ' Operation ', [' xor ', ' lighter ', ' screen ', ' overlay ', ' difference ', ' exclusion ', ' source-over ' ' None ']). Onfinishchange (assignoptions);

Foldermotion.add (Options, ' Motion '). Onfinishchange (assignoptions);

Foldermotion.add (options, ' Speed ', 1, ten). Step (1). Onfinishchange (assignoptions);

Folderparticle.open ();

Document.queryselector ('. Datgui '). appendchild (gui.domelement);

# Sourceurl=pen.js

</script>

</body>

Whether you are a student, a job, or you are trying to find a job, you want to be involved in the field of Web front-end knowledge and improve, there is an official platform for you to communicate and learn from each other ! More small cases waiting for you to download, search Web front-end learning tribe Group Join ...

Mouse Mobile Fireworks

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.