Skip to content

Commit

Permalink
Remove the new Date() stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffposnick committed Jan 22, 2016
1 parent f4a911e commit 615a2e3
Showing 1 changed file with 25 additions and 42 deletions.
67 changes: 25 additions & 42 deletions event-timestamp/demo.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,30 @@
var offsetFromEpoch = 0;
var previousX = 0;
var previousY = 0;
var previousT = 0;

window.addEventListener('load', function(event) {
// Use a simple heuristic to determine the offset from the epoch:
// If the timestamp value is less than the current time,
// then it's almost certainly a DOMHighResTimeStamp. Adding timestamp to
// performance.timing.navigationStart should give a value relative to
// the epoch, which could be passed in to the Date constructor.
// Otherwise, it's a DOMTimeStamp, and is already relative to the epoch.
if (event.timeStamp < Date.now()) {
offsetFromEpoch = performance.timing.navigationStart;
var previousX;
var previousY;
var previousT;

window.addEventListener('mousemove', function(event) {
// Don't update velocity until we have an initial value for X, Y, and T.
if (!(previousX === undefined ||
previousY === undefined ||
previousT === undefined)) {
var Δx = event.screenX - previousX;
var Δy = event.screenY - previousY;
var Δd = Math.sqrt(Math.pow(Δx, 2) + Math.pow(Δy, 2));

// event.timeStamp will always represent a value in milliseconds.
// In supported browsers, the value will have a microsecond resolution, and
// therefore might include a fractional number of milliseconds.
// Older browsers only support a millisecond resolution, and the value will
// always be a whole number.
var Δt = event.timeStamp - previousT;

// Multiply by 1000 to go from milliseconds to seconds.
var velocityInPixelsPerSecond = Δd / Δt * 1000;

ChromeSamples.setStatus(velocityInPixelsPerSecond);
}

// While a 'mousemove' listener is being used for the purposes of this
// example, the new timeStamp resolution applies to all Event types.
window.addEventListener('mousemove', calculateVelocity);
});

function calculateVelocity(event) {
var Δx = event.screenX - previousX;
var Δy = event.screenY - previousY;
var Δd = Math.sqrt(Math.pow(Δx, 2) + Math.pow(Δy, 2));

// event.timeStamp will always represent a value in milliseconds.
// In supported browsers, the value will have a microsecond resolution, and
// therefore might include a fractional number of milliseconds.
// Older browsers only support a millisecond resolution, and the value will
// always be a whole number.
var Δt = event.timeStamp - previousT;

// Multiply by 1000 to go from milliseconds to seconds.
var velocityInPixelsPerSecond = Δd / Δt * 1000;

// Contrived example to illustrate how to use offsetFromEpoch to get a Date
// that corresponds to event.timeStamp.
var date = new Date(event.timeStamp + offsetFromEpoch);

ChromeSamples.setStatus(velocityInPixelsPerSecond + ' as of ' +
date.toLocaleTimeString());

previousX = event.screenX;
previousY = event.screenY;
previousT = event.timeStamp;
}
});

0 comments on commit 615a2e3

Please sign in to comment.