Path: blob/main/projects/circlo/html5game_a5/tph_html5fixes3.js
4626 views
//Source: http://stackoverflow.com/questions/8603656/html5-canvas-arcs-not-rendering-correctly-in-google-chrome1var is_chrome = navigator.userAgent.toLowerCase().indexOf("chrome") > -1;2if (is_chrome) {3CanvasRenderingContext2D.prototype.arc = function (x, y, radius, startAngle, endAngle, anticlockwise) {4// Signed length of curve5var signedLength;6var tau = 2 * Math.PI;78if (!anticlockwise && endAngle - startAngle >= tau) {9signedLength = tau;10} else if (anticlockwise && startAngle - endAngle >= tau) {11signedLength = -tau;12} else {13var delta = endAngle - startAngle;14signedLength = delta - tau * Math.floor(delta / tau);1516// If very close to a full number of revolutions, make it full17if (Math.abs(delta) > 1e-12 && signedLength < 1e-12) signedLength = tau;1819// Adjust if anti-clockwise20if (anticlockwise && signedLength > 0) signedLength = signedLength - tau;21}2223// Minimum number of curves; 1 per quadrant.24var minCurves = Math.ceil(Math.abs(signedLength) / (Math.PI / 2));2526// Number of curves; square-root of radius (or minimum)27var numCurves = Math.ceil(Math.max(minCurves, Math.sqrt(radius)));2829// "Radius" of control points to ensure that the middle point30// of the curve is exactly on the circle radius.31var cpRadius = radius * (2 - Math.cos(signedLength / (numCurves * 2)));3233// Angle step per curve34var step = signedLength / numCurves;3536// Draw the circle37this.lineTo(x + radius * Math.cos(startAngle), y + radius * Math.sin(startAngle));38for (var i = 0, a = startAngle + step, a2 = startAngle + step / 2; i < numCurves; ++i, a += step, a2 += step)39this.quadraticCurveTo(x + cpRadius * Math.cos(a2), y + cpRadius * Math.sin(a2), x + radius * Math.cos(a), y + radius * Math.sin(a));40};41}4243var context_2;44function beginPath(ctx, linewidth) {45ctx.beginPath();46ctx.lineWidth = linewidth;47context_2 = ctx;48}4950function pathMoveTo(x1, y1, x2, y2) {51context_2.lineTo(x1, y1, x2, y2);52}5354function pathCircle(x, y, radius) {55context_2.moveTo(x, y);56context_2.arc(x, y, radius, 0, 2 * Math.PI);57}5859function pathStroke() {60context_2.stroke();61}6263function pathFill() {64context_2.fill();65}6667//Avoid the popup blocker68var urlOpenFunction;69function addLinkHandler(url) {70urlOpenFunction = function (e) {71var keyCode = e.keyCode;72if (keyCode == 13 || keyCode == 32) {73window.open(url, "_blank", "width=1000, height=500, location=yes, resizable=yes, scrollbars=yes, toolbar=yes");74document.getElementById("canvas").focus();75}76};77document.addEventListener("keydown", urlOpenFunction);78}7980function removeLinkHandler() {81document.removeEventListener("keydown", urlOpenFunction);82}8384//Avoid keydown problems85var keys = {};86window.addEventListener(87"keydown",88function (e) {89keys[e.keyCode] = true;90switch (e.keyCode) {91case 37:92case 39:93case 38:94case 40: // Arrow keys95case 32:96e.preventDefault();97break; // Space98default:99break; // do not block other keys100}101},102false103);104window.addEventListener(105"keyup",106function (e) {107keys[e.keyCode] = false;108},109false110);111112function addAnimationPolyfill() {113window.requestAnimFrame = (function () {114return function (callback) {115window.setTimeout(callback, 16);116};117})();118}119120