Bouncing ball code causes jitter in HTML5 canvas

…衆ロ難τιáo~ 提交于 2020-03-05 04:39:33

问题


I have a HTML Canvas on my website that I use to contain balls that drop into the canvas, they bounce around and have a real good time settling down at the bottom in a range of ways.

I was under the impression this was working perfectly. However, it has now been brought to my attention these balls jitter and freak out on other people's computers. I checked the browser they are using and it is the same as mine (Chrome V79). So just to clarify - I have never had it bug on my computer but it consistently has this jitter on other peoples computers, some with more powerful computers as well as lower spec computers.

This is it (this CodePen doesn't have the jitter on their computers, so you may not see it also): https://codepen.io/jason-is-my-name/pen/gObKGRg

This is a video of the bug: https://streamable.com/vtg0g

$(document).ready(function () {
	$.ajaxSetup({
		cache: true
	});
	$.when(
		$.getScript("https://code.createjs.com/easeljs-0.6.0.min.js"),
		$.getScript("https://cdnjs.cloudflare.com/ajax/libs/matter-js/0.10.0/matter.min.js"),
		$.Deferred(function (deferred) {
			$(deferred.resolve);
		})
	).done(function () {
		new App();
	});
});

function App() {
	var self = this;

	self.running = false;
	self.initialized = false;
	var stageClicked = false;
	var stage, canvas;

	var canvasWidth = 410;
	var canvasHeight = 550;
	var ballBounce = 0.8;
	var balls = [];
	var matterJsBalls = [];
	var _gravityY = 1;
	var _gravityX = 0;
	var FPS = 60;
	var infoText, detailsText;
	var ballsInitalized = false;
	var iOS = navigator.userAgent.match(/(iPod|iPhone|iPad)/);
	var startTime = (new Date()).getTime();

	var engine = Matter.Engine.create();

	var world = engine.world;

	var floor = Matter.Bodies.rectangle(canvasWidth / 2, canvasHeight + 50, canvasWidth, 100, {
		isStatic: true,
		render: {
			visible: false
		}
	});
	var leftWall = Matter.Bodies.rectangle(-50, canvasHeight / 2, 100, canvasHeight, {
		isStatic: true,
		render: {
			visible: false
		}
	});
	var rightWall = Matter.Bodies.rectangle(canvasWidth + 50, canvasHeight / 2, 100, canvasHeight, {
		isStatic: true,
		render: {
			visible: false
		}
	});

	Matter.World.add(world, [floor, leftWall, rightWall]);

	self.initialize = function () {
		toggleListeners(true);
		self.initCanvas();
		self.initGame();
	};

	var toggleListeners = function (enable) {
		if (!enable) return;
	};

	self.refresh = function () {};

	self.initCanvas = function () {
		canvas = $("#ball-stage").get(0);
		stage = new createjs.Stage(canvas);

		window.addEventListener("resize", onStageResize, false);
		onStageResize();
		createjs.Touch.enable(stage);
		createjs.Ticker.addListener(tick);
		createjs.Ticker.setFPS(FPS);

		self.initialized = true;
	};

	self.initGame = function () {
		initBalls(canvasWidth, canvasHeight);
	};

	var onStageResize = function () {
		stage.canvas.width = canvasWidth;
		stage.canvas.height = canvasHeight;
	};

	var initBalls = function (stageX, stageY) {
		var imagesArray = [
	"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3-320-80.jpg",
	"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3-320-80.jpg",
	"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3-320-80.jpg",
	"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3-320-80.jpg",
	"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3-320-80.jpg",
	"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3-320-80.jpg",
	"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3-320-80.jpg",
	"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3-320-80.jpg",
	"https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3-320-80.jpg",
   "https://cdn.mos.cms.futurecdn.net/vChK6pTy3vN3KbYZ7UU7k3-320-80.jpg"
		];

		for (var i = 0; i < imagesArray.length; i++) {
			(function (imageArray) {

				setTimeout(function () {
					var arrayImage = new Image();

					arrayImage.onload = function () {
						addBall(arrayImage, stageX / 2, 52);
					};

					arrayImage.src = imageArray;
				}, (i * 1000) + 1000);
			})(imagesArray[i]);
		}
	};

	var addBall = function (img, x, y) {
		var shape = new createjs.Shape();
		shape.id = balls.length;
		shape.radius = 51.25;
		shape.mass = shape.radius;
		shape.x = x;
		shape.y = y;
		shape.vx = rand(-3, 3);
		shape.vy = rand(-3, 3);
		shape.stuck = false;

		var transform = new createjs.Matrix2D();
		transform.appendTransform(-shape.radius, -shape.radius, 1, 1, 0);
		shape.graphics.beginBitmapFill(img, "repeat", transform).drawCircle(0, 0, shape.radius);

		stage.addChild(shape);
		balls.push(shape);

		var circle = Matter.Bodies.circle(x, y, shape.radius, {
			isStatic: false,
			restitution: ballBounce
		});

		Matter.World.add(world, circle);

		Matter.Body.applyForce(circle, {
			x: circle.position.x,
			y: circle.position.y
		}, {
			x: shape.vx * 0.05,
			y: shape.vy * 0.05
		});

		matterJsBalls.push(circle);
	};

	var tick = function () {
		Matter.Engine.update(engine, 16);
		for (var i = 0; i < matterJsBalls.length; i++) {
			var curBall = balls[i];
			var curMatterJsBall = matterJsBalls[i];
			curBall.x = curMatterJsBall.position.x;
			curBall.y = curMatterJsBall.position.y;
		}
		stage.update();
	};

	var rand = function (min, max) {
		return Math.random() * (max - min) + min;
		return Math.random() * max + min;
	};

	self.initialize();
	return self;
}

window.log = function f() {
	log.history = log.history || [];
	log.history.push(arguments);
	if (this.console) {
		var args = arguments,
			newarr;
		args.callee = args.callee.caller;
		newarr = [].slice.call(args);

		if (typeof console.log === "object")
			log.apply.call(console.log, console, newarr);
		else console.log.apply(console, newarr);
	}
};
(function (a) {
	function b() {}

	for (
		var c = "assert,count,debug,dir,dirxml,error,exception,group,groupCollapsed,groupEnd,info,log,markTimeline,profile,profileEnd,time,timeEnd,trace,warn".split(
				","
			),
			d; !!(d = c.pop());

	) {
		a[d] = a[d] || b;
	}
})(
	(function () {
		try {
			console.log();
			return window.console;
		} catch (a) {
			return (window.console = {});
		}
	})()
);
.tech-stack-icons-container{width:410px;height:100%}#ball-stage{width:100%;height:100%;background-color:pink;}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="tech-stack-icons-container">
	<canvas id="ball-stage"></canvas>
</div>

How can I find a fix to this invisible issue?

来源:https://stackoverflow.com/questions/59720514/bouncing-ball-code-causes-jitter-in-html5-canvas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!