Guitar string
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 - 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(); });
})()
Developer | Wenting Zhang |
Username | wentin |
Uploaded | November 07, 2022 |
Rating | 3 |
Size | 3,001 Kb |
Views | 16,192 |
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!
Name | Size |
A Pen by Wenting Zhang | 1,570 Kb |
Underline svg | 1,605 Kb |
Algorithmic Animation with Javascript Tail | 2,356 Kb |
Pure css solution to responsive background image | 1,499 Kb |
Circle button animation for huffpost | 1,700 Kb |
Comedy Central Loading SVG | 7,744 Kb |
Algorithmic Animation javascript template | 1,865 Kb |
Responsify images | 2,140 Kb |
Profession.is type transitions | 12,481 Kb |
Water ripple effect. | 2,746 Kb |
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!
Name | Username | Size |
CSS Heart Loaders | Nourabusoud | 2,161 Kb |
Mondrian | Chrisarmstrong | 2,787 Kb |
A Pen by Alex Bergin | Abergin | 3,347 Kb |
Pure CSS Read More Arrow | Zephyr | 1,747 Kb |
Cars going | Netoguimaraes | 1,699 Kb |
Scrolling Horizontal Isotope | Bramus | 2,017 Kb |
Bootstrap 3 Price Table | Honglio | 2,655 Kb |
BSP Dungeon Generation | Xgundam05 | 5,326 Kb |
Flat UI Button | Honchoman | 2,289 Kb |
Video Player Custom Controls | EleftheriaBatsou | 3,665 Kb |
Surf anonymously, prevent hackers from acquiring your IP address, send anonymous email, and encrypt your Internet connection. High speed, ultra secure, and easy to use. Instant setup. Hide Your IP Now!