Skip to content

Instantly share code, notes, and snippets.

@vaseker
Last active August 29, 2015 14:06
Show Gist options
  • Save vaseker/01f2518b6be3d6196a03 to your computer and use it in GitHub Desktop.
Save vaseker/01f2518b6be3d6196a03 to your computer and use it in GitHub Desktop.
FF SVG bug demo
<!DOCTYPE html>
<html>
<head>
<title>F1 track</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=Edge">
</head>
<body>
<object data="../src/track.min.svg" type="image/svg+xml" id="track"></object>
<input type="button" value="Start" onclick="start()">
<input type="button" value="Pause" onclick="pause()">
<input type="button" value="Segment1" onclick="segment(195)">
<input type="button" value="Segment2" onclick="segment(490)">
<script src="../src/js/script.js"></script>
</body>
</html>
var carDistance = 0,
move = (function () {
var map = document.getElementById('track'), svgContent, car, path, start, startPoint, carStartPoint, trackLength, pos, posP = {};
if (!map) {
return;
}
var onload = function () {
svgContent = map.contentDocument;
car = svgContent.getElementById('Car');
path = svgContent.getElementById('Track');
start = svgContent.getElementById('Start');
carStartPoint = { x: 494, y: 352 };
startPoint = carStartPoint.x + ',' + carStartPoint.y;
pos = path.getPointAtLength(carDistance);
trackLength = path.getTotalLength();
};
if (map.contentDocument && map.contentDocument.URL !== 'about:blank') {
setTimeout(onload, 0);
}
map.addEventListener('load', onload, false);
return function () {
if (!car || !path) {
return;
}
carDistance += 2.5;
if (carDistance > trackLength) {
carDistance = 0;
}
var pos1 = path.getPointAtLength(carDistance),
angle = Math.atan2(pos.y - pos1.y, pos.x - pos1.x) * 180 / Math.PI;
pos = pos1;
var x = posP.x = pos.x - carStartPoint.x,
y = posP.y = pos.y - carStartPoint.y;
car.setAttribute('transform', 'translate(' + x + ', ' + y + ')rotate(' + angle + ',' + startPoint + ')');
};
})(),
animation,
start = function () {
animation = setInterval(move, 50);
},
pause = function () {
clearInterval(animation);
},
segment = function (distance) {
carDistance = distance;
move();
};
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment