cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              <main class="main">
  <canvas id="canvas"></canvas>
</main>
            
          
!
            
              (function(){
	let canvas;
	let context;
	let width = 320;
	let height = 320;
	let mouse = {
		position: {
			x: 100,
			y: 100
		},
		offset: {
			x: 100,
			y: 100
		}
	};
	let particles = [];
	const NUM_OF_PARTICLES = 10;
	let requestAnimationFrame = 
		window.requestAnimationFrame ||
     window.mozRequestAnimationFrame ||
        window.webkitRequestAnimationFrame ||
     window.msRequestAnimationFrame;
    window.requestAnimationFrame = requestAnimationFrame;

	document.addEventListener("DOMContentLoaded", ()=>{
		canvas = document.getElementById("canvas");
		canvas.width = width;
		canvas.height = height;
		context = canvas.getContext("2d");
		context.fillStyle = "rgb(0, 0, 0)";
		context.fillRect(0, 0, width, height);

		canvas.addEventListener("mousemove", (e)=>{
			mouse_offset_move(e)
		});

		set_up_particles();

		draw();
	});

	function set_up_particles(){
		for(let i = 0; i < NUM_OF_PARTICLES; i += 1){
			let stalker_circle = {
				position: {
					x: 100,
					y: 100
				},
				angle: 0,
				radius: 3,
				speed: 0.05+Math.random()*0.05,
				orbit: 1 + Math.random()* 3,
				color: "rgb(" + Math.floor(Math.random()*255) + "," + Math.floor(Math.random()*255) + "," + Math.floor(Math.random()*255) + ")"
			};
			particles[i] = stalker_circle;
		}
	}

	function draw(){
		mouse_position_move()

		draw_canvas_layer();

		draw_mouse_stalkers();

		requestAnimationFrame(draw);
	}

	function mouse_position_move(){
		let sx = mouse.offset.x;
		let sy = mouse.offset.y;
		if(mouse.position.x !== sx &&
			mouse.position.y !== sy){
			mouse.position.x = sx;
			mouse.position.y = sy;
		}
	}

	function mouse_offset_move(e){
		mouse.offset.x = e.x;
		mouse.offset.y = e.y;

		return {
			x: e.x,
			y: e.y
		}
	}

	function draw_canvas_layer(){
		context.fillStyle = "rgba(0, 0, 0, .1)";
		context.fillRect(0, 0, width, height);
	}

	function draw_mouse_stalkers(){
		for(let i = 0; i < NUM_OF_PARTICLES; i += 1){
			let particle = particles[i];
			// 軌道上の(x,y)を作成
			particle.angle += particle.speed;

			let orbit_x = Math.cos(i + particle.angle) * particle.orbit;
			let orbit_y = Math.sin(i + particle.angle) * particle.orbit;
				
			// マウスの座標に向けて移動し、移動した点を中心とし軌道上に座標を打つ
			particle.position.x += (mouse.position.x - particle.position.x) * particle.speed + orbit_x;
			particle.position.y += (mouse.position.y - particle.position.y) * particle.speed + orbit_y;

			context.beginPath();
			context.arc(particle.position.x, particle.position.y, particle.radius, 0, Math.PI*2);
			context.fillStyle = particle.color;
			context.fill();

		}
	}
}());
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console