Skip to content

Commit f1a60f0

Browse files
naaskingmkucko
authored andcommitted
mouseWheelDispatch doc: add example
Add example of interactive, map-like zooming to documentation. [skip ci]
1 parent d57443a commit f1a60f0

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/controls/inputs.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,11 +484,61 @@ Crafty.extend({
484484
* (see [details](http://stackoverflow.com/questions/5527601/normalizing-mousewheel-speed-across-browsers)).
485485
*
486486
* @example
487+
* Zoom the viewport (camera) in response to mouse scroll events.
487488
* ~~~
488489
* Crafty.bind("MouseWheelScroll", function(evt) {
489490
* Crafty.viewport.scale(Crafty.viewport._scale * (1 + evt.direction * 0.1));
490491
* });
491492
* ~~~
493+
*
494+
* @example
495+
* Interactive, map-like zooming of the viewport (camera) in response to mouse scroll events.
496+
* ~~~
497+
* // sign public void zoomTowards(Number amt, Number posX, Number posY, Number time[, String|function easingFn])
498+
* // param Number amt - amount to zoom in on the target by (eg. `2`, `4`, `0.5`)
499+
* // param Number posX - the x coordinate to zoom towards
500+
* // param Number posY - the y coordinate to zoom towards
501+
* // param Number time - the duration in ms of the entire zoom operation
502+
* // param easingFn - A string or custom function specifying an easing.
503+
* // (Defaults to linear behavior.)
504+
* // See `Crafty.easing` for more information.
505+
* //
506+
* // Zooms the camera towards a given point, preserving the current center.
507+
* // `amt > 1` will bring the camera closer to the subject,
508+
* // `amt < 1` will bring it farther away,
509+
* // `amt = 0` will reset to the default zoom level.
510+
* // Zooming is multiplicative. To reset the zoom amount, pass `0`.
511+
* //
512+
* // <example>
513+
* // // Make the entities appear twice as large by zooming in towards (100,100) over the duration of 3 seconds using linear easing behavior
514+
* // zoomTowards(2, 100, 100, 3000);
515+
* // </example>
516+
* //
517+
* function zoomTowards (amt, posX, posY, time, easingFn) {
518+
* var scale = Crafty.viewport._scale,
519+
* // current viewport center
520+
* centX = -Crafty.viewport._x + Crafty.viewport._width / 2 / scale,
521+
* centY = -Crafty.viewport._y + Crafty.viewport._height / 2 / scale,
522+
* // direction vector from viewport center to position
523+
* deltaX = posX - centX,
524+
* deltaY = posY - centY;
525+
* var f = amt - 1;
526+
*
527+
* Crafty.viewport.zoom(amt, centX + deltaX * f, centY + deltaY * f, time, easingFn);
528+
* }
529+
*
530+
* // don't restrict panning of viewport in any way
531+
* Crafty.viewport.clampToEntities = false;
532+
*
533+
* // enable panning of viewport by dragging the mouse
534+
* Crafty.viewport.mouselook(true);
535+
*
536+
* // enable interactive map-like zooming by scrolling the mouse
537+
* Crafty.bind("MouseWheelScroll", function (evt) {
538+
* var pos = Crafty.domHelper.translate(evt.clientX, evt.clientY);
539+
* zoomTowards(1 + evt.direction/10, pos.x, pos.y, 5);
540+
* });
541+
* ~~~
492542
*/
493543
mouseWheelDispatch: function(e) {
494544
e.direction = (e.detail < 0 || e.wheelDelta > 0) ? 1 : -1;

src/graphics/viewport.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,6 @@ Crafty.extend({
788788
onScreen: function (rect) {
789789
return Crafty.viewport._x + rect._x + rect._w > 0 && Crafty.viewport._y + rect._y + rect._h > 0 &&
790790
Crafty.viewport._x + rect._x < Crafty.viewport.width && Crafty.viewport._y + rect._y < Crafty.viewport.height;
791-
},
791+
}
792792
}
793793
});

0 commit comments

Comments
 (0)