Guitar string

Developer
Size
3,001 Kb
Views
16,192

How do I make an guitar string?

What is a guitar string? How do you make a guitar string? This script and codes were developed by Wenting Zhang on 07 November 2022, Monday.

Guitar string Previews

Guitar string - Script Codes HTML Codes

<!DOCTYPE html>
<html >
<head> <meta charset="UTF-8"> <title>guitar string</title> <link rel="stylesheet" href="css/style.css">
</head>
<body> <canvas id="myCanvas"></canvas> <script src="js/index.js"></script>
</body>
</html>

Guitar string - Script Codes CSS Codes

body {	margin: 20px;	padding: 0px;
}
canvas {	background-color: #fff;	margin: auto;	display: block;	width: 100%;	height: 100%;
}

Guitar string - Script Codes JS Codes

function String(id, startPoint, endPoint) {	//ctor	this.canvas = document.getElementById(id);	this.ctx = this.canvas.getContext('2d'); // console.dir(this.canvas); this.canvas.width = this.canvas.clientWidth; this.canvas.height = this.canvas.clientHeight;	this.startPoint = startPoint;	this.endPoint = endPoint;	this.controlPoint = new Point(0,0);	this.lastMouseX = this.controlPoint.x; this.lastMouseY = this.controlPoint.y; this.count = 0; this.damping = 0.98;	this.finishWave = false;	//add event listener	var self = this;	this.canvas.addEventListener('mousemove', function(pos) {	console.dir(pos);	self.mouseMove(self, pos)	}, false);
}
String.prototype.drawArc = function(startPoint, thirdPoint, endPoint, ctx){ var ctx = ctx; var dy1 = thirdPoint.y - startPoint.y; var dx1 = thirdPoint.x - startPoint.x; var dy2 = endPoint.y - thirdPoint.y; var dx2 = endPoint.x - thirdPoint.x; var aSlope = dy1/dx1; var bSlope = dy2/dx2; var centerX = (aSlope*bSlope*(startPoint.y - endPoint.y) + bSlope*(startPoint.x + thirdPoint.x) - aSlope*(thirdPoint.x+endPoint.x) )/( 2* (bSlope-aSlope) ); var centerY = -1*(centerX - (startPoint.x+thirdPoint.x)/2)/aSlope + (startPoint.y+thirdPoint.y)/2; // var centerX = (aSlope*bSlope*(y0 - y2) + bSlope*(x0 + x1) // - aSlope*(x1+x2) )/( 2* (bSlope-aSlope) ); // var centerY = -1*(centerX - (x0+x1)/2)/aSlope + (y0+y1)/2; var r = dist(centerX, centerY, startPoint.x, startPoint.y) var angle = Math.atan2(centerX-startPoint.x, centerY-startPoint.y); // console.log(angle); if (!angle || this.finishWave){ ctx.beginPath(); ctx.moveTo(startPoint.x, startPoint.y); ctx.lineTo(endPoint.x, endPoint.y); // this.finishWave = true; } else if (!this.finishWave){	if( angle > Math.PI/2) { ctx.beginPath(); ctx.arc(centerX, centerY, r, Math.PI * 1.5-angle, Math.PI * 1.5 + angle, true); } else { ctx.beginPath(); ctx.arc(centerX, centerY, r, Math.PI * 1.5-angle, Math.PI * 1.5 + angle, false); } } // ctx.rect(centerX, centerY, 2, 2); // ctx.rect(startPoint.x, startPoint.y, 2, 2); // ctx.rect(endPoint.x, endPoint.y, 2, 2); ctx.stroke();
}
String.prototype.draw = function(){	// draw stuff var r = circleCenter(	new Point(this.startPoint.x, this.startPoint.y),	new Point(this.controlPoint.x, this.controlPoint.y),	new Point(this.endPoint.x, this.endPoint.y) ).r; if( r > 600	&& this.controlPoint.x > this.startPoint.x	&& this.controlPoint.x < this.endPoint.x ){ // console.log(r); this.drawArc(this.startPoint, this.controlPoint, this.endPoint, this.ctx); this.lastMouseX = this.controlPoint.x; this.lastMouseY = this.controlPoint.y; this.count = 0;	this.finishWave = false; } else {	var x = this.lastMouseX;	var y = this.startPoint.y+(this.lastMouseY-this.startPoint.y)	*Math.cos(this.count/5*Math.PI)*Math.pow(this.damping, this.count);	if ( Math.pow(this.damping, this.count) < 0.0001) {	this.finishWave = true;	}	var wavePoint = new Point(x, y); this.drawArc(this.startPoint, wavePoint, this.endPoint, this.ctx); this.count++; }
};
String.prototype.clear = function(){	// clear	this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
};
String.prototype.update = function(){	// update
};
String.prototype.mouseMove = function(self, pos){	self.controlPoint.x = pos.layerX;	self.controlPoint.y = pos.layerY;
};
/*Created By Wenting Zhang
*/
var dist = function(x, y, x0, y0){ return Math.sqrt((x -= x0) * x + (y -= y0) * y);
};
var circleCenter = function(startPoint, thirdPoint, endPoint){ var dy1 = thirdPoint.y - startPoint.y; var dx1 = thirdPoint.x - startPoint.x; var dy2 = endPoint.y - thirdPoint.y; var dx2 = endPoint.x - thirdPoint.x; var aSlope = dy1/dx1; var bSlope = dy2/dx2; var centerX = (aSlope*bSlope*(startPoint.y - endPoint.y) + bSlope*(startPoint.x + thirdPoint.x) - aSlope*(thirdPoint.x+endPoint.x) )/( 2* (bSlope-aSlope) ); var centerY = -1*(centerX - (startPoint.x+thirdPoint.x)/2)/aSlope + (startPoint.y+thirdPoint.y)/2; var r = dist(centerX, centerY, startPoint.x, startPoint.y) // return [centerX, centerY, r]; return { x: centerX, y: centerY, r: r };
}
var Point = function (x,y){ this.x=x; this.y=y;
}
window.requestAnimFrame = (function(callback) {	return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||	function(callback) { window.setTimeout(callback, 1000 / 60);	};
})();
var myString = new String('myCanvas', new Point(300, 100.5), new Point(600, 100.5));
(function animate() {	// update	myString.update();	// clear myString.clear();	// draw stuff // console.log("run every frame"); myString.draw();	// request new frame	requestAnimFrame(function() { animate();	});
})()
Guitar string - Script Codes
Guitar string - Script Codes
Home Page Home
Developer Wenting Zhang
Username wentin
Uploaded November 07, 2022
Rating 3
Size 3,001 Kb
Views 16,192
Do you need developer help for Guitar string?

Find the perfect freelance services for your business! Fiverr's mission is to change how the world works together. Fiverr connects businesses with freelancers offering digital services in 500+ categories. Find Developer!

Wenting Zhang (wentin) Script Codes
Create amazing web content with AI!

Jasper is the AI Content Generator that helps you and your team break through creative blocks to create amazing, original content 10X faster. Discover all the ways the Jasper AI Content Platform can help streamline your creative workflows. Start For Free!