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.

Quick-add: + add another resource

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.

Quick-add: + add another resource

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.

            
              <div class="global">
		<canvas class="canvas"></canvas>
	</div>
            
          
!
            
              .global{
	width: 100%;
	height: 100%;
}

.canvas{
	position: absolute;
	top: 50%;
	left: 50%;
	margin: auto;
	background-color: rgb(0, 0, 0);
	transform: translate(-50%, -50%);
}
            
          
!
            
              (function(){
	let $ = {
		mouse:{
			x: 0,
			y: 0,
			flag: false
		},
		color: {
			white: "255,255,255",
			red: "211,47,47"
		},
		particle:{
			radius: 16,
		},
		width: 480,
		height: 480,
		sprite: ""
	};
	let requestAnimationFrame = window.requestAnimationFrame || 
		window.mozRequestAnimationFrame ||
		window.webkitRequestAnimationFrame || 
		window.msRequestAnimationFrame;
    window.requestAnimationFrame = requestAnimationFrame;

    let cancelAnimationFrame = window.cancelAnimationFrame ||
    	window.mozcancelAnimationFrame ||
		window.webkitcancelAnimationFrame ||
		window.mscancelAnimationFrame;
    window.cancelAnimationFrame = cancelAnimationFrame;


	// touch or click
	const support_touch = "ontouchend" in document;
	const TOUCH_START = support_touch ? "touchstart": "mousedown";
	const TOUCH_MOVE = support_touch ? 'touchmove' : 'mousemove';
	const TOUCH_END = support_touch ? 'touchend' : 'mouseup';

	document.addEventListener("DOMContentLoaded", ()=>{
		$.canvas = document.querySelector(".canvas");
		$.canvas.width = $.width;
		$.canvas.height = $.height;
		$.ctx = $.canvas.getContext("2d");

		// canvas event
		$.canvas.addEventListener(TOUCH_START, $.touch_start);
		$.canvas.addEventListener(TOUCH_MOVE, $.touch_move);
		$.canvas.addEventListener(TOUCH_END, $.touch_end);

		$.set_up();

		$.draw();
	});

	$.touch_start = function(e) {
		let x;
		let y;
		let rect = $.canvas.getBoundingClientRect();
		let offset_left = Math.floor(rect.left);
		let offset_top = Math.floor(rect.top);
		
		$.mouse.flag = true;
		e.preventDefault();	// タッチによる画面スクロールを止める

		if(e.pageX || e.pageY){
			x = e.pageX;
			y = e.pageY;
		}else{
			x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft; 
  			y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop; 
		}
		$.mouse.x = x - offset_left;
		$.mouse.y = y - offset_top;

		$.sprite.touch_start();
	};

	$.touch_move = function(e) {
		let x;
		let y;
		let rect = $.canvas.getBoundingClientRect();
		let offset_left = rect.left;
		let offset_top = rect.top;

		e.preventDefault();

		if(e.pageX || e.pageY){
			x = e.pageX;
			y = e.pageY;
		}else{
			x = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft; 
  			y = e.clientY + document.body.scrollTop + document.documentElement.scrollTop; 
		}

		$.mouse.x = x - offset_left;
		$.mouse.y = y - offset_top;

		$.sprite.touch_move();
	};

	$.touch_end = function(e) {
		$.mouse.flag = false;
		e.preventDefault();

		$.sprite.touch_end();
	};

	$.set_up = function() {
		$.mouse.x = $.width / 2;
		$.mouse.y = $.height / 2;
		$.sprite = new Particle($.mouse, $.particle.radius, $.color.red);
	}

	$.draw = function(){
		requestAnimationFrame($.draw);

		$.sprite.clean();

		$.sprite.draw();
	}

	class Particle{
		constructor(position, radius, color){
			this.radius = radius;
			this.color = color || $.color.white;
			this.is_touch = false;
			this.x = position.x;
			this.y = position.y;
			this.ideal_point = {
				x: this.x,
				y: this.y
			};
			this.p1 = {
				x: 0,
				y: 0
			};
			this.p2 = {
				x: 0,
				y: 0
			};
			this.vx = 0;
			this.vy = 0;
			this.flag_inertia = {
				x: false,
				y: false	
			};
		}
		touch_start(){
			this.p1.x = this.p2.x = this.ideal_point.x = $.mouse.x;
			this.p1.y = this.p2.y = this.ideal_point.y = $.mouse.y;
			this.is_touch = true;
		}
		touch_move(){
			if(this.is_touch){
				this.p1.x = this.p2.x;
				this.p1.y = this.p2.y;

				this.p2.x = this.ideal_point.x = $.mouse.x;
				this.p2.y = this.ideal_point.y = $.mouse.y;
			}
		}
		touch_end(){
			let dx = this.p2.x - this.p1.x;
			let dy = this.p2.y - this.p1.y;

			this.vx = dx * 2.5;
			this.vy = dy * 2.5;

			this.flag_inertia.x = true;
			this.flag_inertia.y = true;
			this.is_touch = false;
		}
		move(){
			if(this.flag_inertia.x){
				if(this.x + this.vx - this.radius >= $.width){
					this.vx *= -1;
				}else if(this.x + this.vx - this.radius <= 0){
					this.vx *= -1;
				}else{
					this.x += this.vx;
				}
				this.vx /= 1.15;
				if(this.vx <= 1 && this.vx >= -1){
					this.ideal_point.x = this.x;
					this.flag_inertia.x = false;
				}
			}
			if(this.flag_inertia.y){
				if(this.y + this.vy - this.radius >= $.height){
					this.vy *= -1;
				}else if(this.y + this.vy - this.radius <= 0){
					this.vy *= -1;
				}else{
					this.y += this.vy;
				}
				this.vy /= 1.15;
				if(this.vy <= 1 && this.vy >= -1){
					this.ideal_point.y = this.y;
					this.flag_inertia.y = false;
				}
			}
			if(!this.flag_inertia.x && !this.flag_inertia.y){
				this.x = this.ideal_point.x;
				this.y = this.ideal_point.y;
			}
		}
		draw(){
			this.move();
			$.ctx.beginPath();
			$.ctx.arc(this.x, this.y, this.radius, 0, Math.PI*2, false);
			$.ctx.fillStyle = "rgb(" + this.color + ")";
			$.ctx.fill();
		}
		clean(){
			// 描画をクリアにする
			$.ctx.globalCompositeOperation = "destination-out";
		    $.ctx.beginPath();
		    $.ctx.arc(this.x, this.y, this.radius + 1, 0, 2 * Math.PI, false);
		    $.ctx.fill();
		    $.ctx.globalCompositeOperation = "source-over"
		}
	}
})();
            
          
!
999px
Loading ..................

Console