为了好玩,我试图创建不同排序算法的可视化,但我遇到了Canvas动画的问题。
我假设我只能在分拣器方法中调用绘图函数,但这会导致浏览器锁定,直到数组完全排序,然后绘制一些中间框架。
我如何从排序方法中进行动画制作?下面是我到目前为止的代码,我不会运行此代码片段,因为它会将标签挂起几秒钟。
N = 250; // Array Size
XYs = 5; // Element Visual Size
Xp = 1; // Start Pos X
Yp = 1; // Start Pos Y
var canvas;
var l = Array.apply(null, {
length: N
}).map(Number.call, Number);
Array.prototype.shuffle = function() {
var i = this.length,
j, temp;
if (i == 0) return this;
while (--i) {
j = Math.floor(Math.random() * (i + 1));
temp = this[i];
this[i] = this[j];
this[j] = temp;
}
return this;
}
function map_range(x, in_min, in_max, out_min, out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
function rainbow(x) {
var m = map_range(x, 0, N, 0, 359);
return 'hsl(' + m + ',100%,50%)';
}
function init() {
canvas = document.getElementById('canvas');
l.shuffle();
draw();
bubbleSort(l);
}
function draw() {
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
for (var i = 0; i < l.length; i++) {
ctx.fillStyle = rainbow(l[i]);
ctx.fillRect((Xp * i) * XYs, Yp * XYs, XYs, XYs);
}
}
}
function bubbleSort(a) {
var swapped;
do {
swapped = false;
for (var i = 0; i < a.length - 1; i++) {
if (a[i] > a[i + 1]) {
var temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
swapped = true;
draw();
setTimeout(function() {}, 10);
}
}
} while (swapped);
}
&#13;
<html>
<body onload="init();">
<canvas id="canvas" width="1500" height="1500"></canvas>
</body>
</html>
&#13;
答案 0 :(得分:5)
一个解决方案是ES6 Generator function*
及其<?php
header('Content-Type: application/json');
$e = "90";
// You would calculate a real value here
echo json_encode([
'diffex' => $e
]);
?>
语句。
这允许您暂停一个函数并在稍后暂停的地方重新启动它:
yield
&#13;
N = 100; // Array Size
XYs = 5; // Element Visual Size
Xp = 1; // Start Pos X
Yp = 1; // Start Pos Y
var canvas;
var l = Array.apply(null, {
length: N
}).map(Number.call, Number);
Array.prototype.shuffle = function() {
var i = this.length,
j, temp;
if (i == 0) return this;
while (--i) {
j = Math.floor(Math.random() * (i + 1));
temp = this[i];
this[i] = this[j];
this[j] = temp;
}
return this;
}
function map_range(x, in_min, in_max, out_min, out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
function rainbow(x) {
var m = map_range(x, 0, N, 0, 359);
return 'hsl(' + m + ',100%,50%)';
}
function init() {
canvas = document.getElementById('canvas');
l.shuffle();
var sort = bubbleSort(l);
// an anim function triggered every 60th of a second
function anim(){
requestAnimationFrame(anim);
draw();
sort.next(); // call next iteration of the bubbleSort function
}
anim();
}
function draw() {
if (canvas.getContext) {
var ctx = canvas.getContext('2d');
for (var i = 0; i < l.length; i++) {
ctx.fillStyle = rainbow(l[i]);
ctx.fillRect((Xp * i) * XYs, Yp * XYs, XYs, XYs);
}
}
}
function* bubbleSort(a) { // * is magic
var swapped;
do {
swapped = false;
for (var i = 0; i < a.length - 1; i++) {
if (a[i] > a[i + 1]) {
var temp = a[i];
a[i] = a[i + 1];
a[i + 1] = temp;
swapped = true;
yield swapped; // pause here
}
}
} while (swapped);
}
init();
&#13;
答案 1 :(得分:1)
你需要一个渲染循环。 requestAnimationFrame()是你的朋友。使用此方法,您可以为浏览器提供在下一帧绘制之前调用的回调。
在该回调中,您可以使用相同的回调来绘制您的内容并再次调用requestAnimationFrame():
requestAnimationFrame(renderLoop);
function renderLoop()
{
// visualize your array `l` at this point
// call the render callback for the next frame draw
requestAnimationFrame(renderLoop);
}
https://developer.mozilla.org/de/docs/Web/API/window/requestAnimationFrame
您可以获得平滑的动画,帧速率通常为60 fps。
在代码中集成该方法:
删除行
draw();
setTimeout(function() {}, 10);
调用初始
requestAnimationFrame(renderLoop);
程序启动时。