Cara Membuat Animasi Silly String dengan Javascript

Berfokus pada bidang desain serta artistik, kehadiran P5.js sebagai subsider dari Javascript selalu memberikan inspirasi tersendiri bagi para pengembang web. Seringkali, penggunaan P5.js dilibatkan dalam pemberian efek background (maupun transisi) pada suatu website. Juga, para pengembang web sering berinteraksi dengan bahasa pemrograman terkait sebagai media animasi (melalui berbagai package yang telah mendukung P5.js)

Sebagai presentasi dari fungsionalitas animasi, pada artikel kali ini, kita akan membuat animasi silly string menggunakan bahasa pemrograman P5.js (subsider dari Javascript).

Langkah:

1. Persiapkan text editor (notepad, sublime text, dan sebagainya) sebagai media pengetikan syntax nantinya.

2. Buatlah file yang bernama index.html yang berisikan kode sebagai berikut:

<html>
<head>
  <title>Inwepo Silly String</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.10/p5.js"></script>
  <style>
  </style>
</head>
<body>
  <script>
  </script>
</body>
</html>

3. Pada bagian <style>, masukkan kode berikut:

body {
  padding: 0;
  margin: 0;
  background-color: #222;
}

Nantinya, bagian <style> akan memiliki struktur kode sebagai berikut:

<style>
	body {
	  padding: 0;
	  margin: 0;
	  background-color: #222;
	}
</style>

4. Pada bagian <script>, masukkan kode berikut:

var walkersNum = 600;
var wArray = [];
var newhue;

function setup() {
    createCanvas(windowWidth, windowHeight);
    colorMode(HSB, 360, 100, 100, 100);
    for (var i = 0; i < walkersNum; i++) {
        wArray[i] = new Walker();
    }
    setInterval(resetcolors, 30000);
    resetcolors();
    background(newhue, 50, 20);
}

function header(){
	fill(color("#222"));
	rect(0,0,width,100);
	textFont('Segoe UI');
	textSize(50);
	textAlign(CENTER);
	text("Inwepo Silly String", width/2, 60);
}

function draw() {
    for (var i = 0; i < wArray.length; i++) {
        wArray[i].update();
        wArray[i].display(); 
        wArray[i].transborders();
    }
	header();
}

function Walker() {
    this.pos = createVector(random(width / 2 - 100, width / 2 + 100), random(height / 2 - 100, height / 2 + 100));
    this.vel = createVector(0, 0);
    this.xoof = random(0, 1000);
    this.hue = 360;
    this.sat = 100;
    this.bri = 70;

    this.display = function() {
        var brushSize = map(noise(this.xoof), 0, 1, 2, 8);
        this.bri = noise(this.xoof) * 100;
        strokeWeight(brushSize);
        stroke(this.hue, this.sat, this.bri);
        point(this.pos.x, this.pos.y);
    }

    this.update = function() {
        this.xoof += 0.01;
        this.acc = p5.Vector.fromAngle(map(noise(this.xoof), 0, 1, -TWO_PI, TWO_PI));
        this.acc.mult(0.5);

        this.vel.add(this.acc); 
        this.vel.limit(2); 
        this.pos.add(this.vel);
    }

    this.changecolor = function(newhue) {
        this.hue = newhue;
    }

    this.transborders = function() {
        if (this.pos.x > width) {
            this.pos.x = 0;
        } else if (this.pos.x < 0) {
            this.pos.x = width;
        }
        if (this.pos.y > height) {
            this.pos.y = 0;
        } else if (this.pos.y < 0) {
            this.pos.y = height;
        }
    }

}

function mousePressed() {
    restartString();
}

function windowResized() {
    resizeCanvas(windowWidth, windowHeight);
    restartString();
}

function restartString(){
    for (var i = 0; i < walkersNum; i++) {
        wArray[i] = new Walker();
    }
    resetcolors();
    background(newhue, 50, 20);
}

function resetcolors() {
    console.log("resetting global color");
    newhue = random(360);
    for (var i = 0; i < wArray.length; i++) {
        wArray[i].changecolor(newhue);
    }
}

Nantinya, bagian <script> akan memiliki struktur kode sebagai berikut:

<script>
	var walkersNum = 600;
	var wArray = [];
	var newhue;

	function setup() {
		createCanvas(windowWidth, windowHeight);
		colorMode(HSB, 360, 100, 100, 100);
		for (var i = 0; i < walkersNum; i++) {
			wArray[i] = new Walker();
		}
		setInterval(resetcolors, 30000);
		resetcolors();
		background(newhue, 50, 20);
	}

	function header(){
		fill(color("#222"));
		rect(0,0,width,100);
		textFont('Segoe UI');
		textSize(50);
		textAlign(CENTER);
		text("Inwepo Silly String", width/2, 60);
	}

	function draw() {
		for (var i = 0; i < wArray.length; i++) {
			wArray[i].update();
			wArray[i].display(); 
			wArray[i].transborders();
		}
		header();
	}

	function Walker() {
		this.pos = createVector(random(width / 2 - 100, width / 2 + 100), random(height / 2 - 100, height / 2 + 100));
		this.vel = createVector(0, 0);
		this.xoof = random(0, 1000);
		this.hue = 360;
		this.sat = 100;
		this.bri = 70;

		this.display = function() {
			var brushSize = map(noise(this.xoof), 0, 1, 2, 8);
			this.bri = noise(this.xoof) * 100;
			strokeWeight(brushSize);
			stroke(this.hue, this.sat, this.bri);
			point(this.pos.x, this.pos.y);
		}

		this.update = function() {
			this.xoof += 0.01;
			this.acc = p5.Vector.fromAngle(map(noise(this.xoof), 0, 1, -TWO_PI, TWO_PI));
			this.acc.mult(0.5);

			this.vel.add(this.acc); 
			this.vel.limit(2); 
			this.pos.add(this.vel);
		}

		this.changecolor = function(newhue) {
			this.hue = newhue;
		}

		this.transborders = function() {
			if (this.pos.x > width) {
				this.pos.x = 0;
			} else if (this.pos.x < 0) {
				this.pos.x = width;
			}
			if (this.pos.y > height) {
				this.pos.y = 0;
			} else if (this.pos.y < 0) {
				this.pos.y = height;
			}
		}

	}

	function mousePressed() {
		restartString();
	}

	function windowResized() {
		resizeCanvas(windowWidth, windowHeight);
		restartString();
	}

	function restartString(){
		for (var i = 0; i < walkersNum; i++) {
			wArray[i] = new Walker();
		}
		resetcolors();
		background(newhue, 50, 20);
	}

	function resetcolors() {
		console.log("resetting global color");
		newhue = random(360);
		for (var i = 0; i < wArray.length; i++) {
			wArray[i].changecolor(newhue);
		}
	}	
</script>

5. Save file yang telah dimodifikasi sebelumnya. Lalu, buka file index.html melalui browser kamu. Jika berhasil, nantinya halaman website akan menampilkan konten teks menyertakan animasi silly string dengan pewarnaan serta gerakan acak sesuai dengan syntax yang telah kita tetapkan sebelumnya. Sesuaikan penggunaan animasi silly string dengan website yang telah kamu buat.

Capture

Demikian tutorial cara membuat animasi silly string dengan Javascript. Semoga bermanfaat.

Komentar

Leave a Reply

Your email address will not be published. Required fields are marked *

Trending Minggu Ini

Inwepo adalah media platform yang membantu setiap orang untuk belajar dan berbagi tutorial, tips dan trik cara penyelesaian suatu masalah di kehidupan sehari-hari dalam bentuk teks, gambar. dan video.

Dengan bergabung bersama kami dan membuat 1 tutorial terbaik yang kamu miliki dapat membantu jutaan orang di Indonesia untuk mendapatkan solusinya. Ayo berbagi tutorial terbaikmu.

Ikuti Kami di Sosmed

Inwepo Navigasi

Tentang Kami             Beranda

Hubungi Kami             Panduan Penulis

Kebijakan Privasi

FAQ

Partner

Copyright © 2014 - 2023 Inwepo - All Rights Reserved.

To Top