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.

            
              	<main class="main">
    <div class="form">
			<input type="text" value="codepen" name="character"/>
			<input type="button" value="変更" name="btn" />
		</div>
		<canvas id="canvas"></canvas>
	</main>

            
          
!
            
              (function(){
	let $ = window.$ = {};
	const __$ = window.__$ = {};
	let temp_canvas;
	let temp_context;
	$.canvas;
	$.context;
	let character;
	let $btn;
	let $frm_text;
	let particles = [];
	let animation_id;
	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;

	__$.WIDTH = 500;
	__$.HEIGHT = 150;
	__$.RED = "rgb(255,0,0)";
	__$.BLUE = "rgb(0, 0, 255)";
	__$.RADIUS = 1.5;
	__$.INTERVAL = 300;

	document.addEventListener("DOMContentLoaded", ()=>{
		temp_canvas = document.createElement("canvas");
		temp_context = temp_canvas.getContext("2d");

		$.canvas = document.getElementById("canvas");
		$.context = canvas.getContext("2d");

		$.canvas.width = temp_canvas.width = __$.WIDTH;
		$.canvas.height = temp_canvas.height = __$.HEIGHT;

		$frm_text = document.getElementsByName("character")[0];
		$btn = document.getElementsByName("btn")[0];
		$btn.addEventListener("click", ()=>{
			refresh();
		})

		init_pixel_character();

		generate_from_pixel_data();
	});

	function refresh() {
		cancelAnimationFrame(animation_id);

		$.context.clearRect(0, 0, __$.WIDTH, __$.HEIGHT);
		$.count = -50;
		particles = [];
		
		init_pixel_character();

		generate_from_pixel_data();
	}

	// temp canvasに文字を描画する
	function init_pixel_character(){
		temp_context.clearRect(0, 0, __$.WIDTH, __$.HEIGHT);
		character = $frm_text.value.slice(0, 10);
		// fontのスタイル、サイズ、種類の指定
		temp_context.font = "bold 50px sans-serif";
		// textの揃え位置の指定
		temp_context.textAlign = "center";
		// textの上下の基準線の指定
		// temp_context.textBaseline = "middle"
		temp_context.fillStyle = "rgb(255,255,255)";
		temp_context.fillText(character, __$.WIDTH / 2, __$.HEIGHT / 2);
	}

	// temp canvasのピクセルデータをもとにパーティクル(x,y)を生成する
	// ピクセルnは
	// (n*4): 0~255(r)
	// (n*4)+1: 0~255(g)
	// (n*4)+2: 0~255(b)
	// (n*4)+3: 0~255(a)
	// のようになっている
	function generate_from_pixel_data(){
		let data = temp_context.getImageData(0, 0, __$.WIDTH, __$.HEIGHT).data;
		let diameter = __$.RADIUS * 2;
		let index = 0;
		for(let i = 0, iz = __$.WIDTH - diameter; i < iz; i += diameter){
			for(let j = 0, jz = __$.HEIGHT - diameter; j < jz; j += diameter){
				// 透明ではないならパーティクルを生成する
				let threshold = data[((i + j * __$.WIDTH) * 4) + 3];
				if(threshold === 0){
					continue;
				}
				particles[index] = 	new Particle(i, j);
				index += 1;
			}
		}
	}
  
  class Particle{
	constructor(x, y, index){
		this.x = x
		this.y = y
		this.v = 15;
		this.draw();
	}
	draw(bool){
		$.context.beginPath();
		$.context.arc(this.x, this.y, __$.RADIUS, 0, Math.PI*2);
		if(bool){
			$.context.fillStyle = __$.BLUE;
		}else{
			$.context.fillStyle = __$.RED;
		}
		$.context.fill();
	}
}
})();
            
          
!
999px
Loading ..................

Console