1.0.18 • Published 8 years ago

angular-pan-zoom v1.0.18

Weekly downloads
130
License
-
Repository
github
Last release
8 years ago

angular-pan-zoom

AngularJS directive for implementing pan and zoom on any DOM element

Build Status

NOTE: Migration from 0.9.0 to 1.0.0

The 1.0.0 release introduced a breaking change in how the panzoom widget publishes its API. Migration guide here

Getting it:

Get the code from github. Or simpler yet, use bower:

bower install angular-pan-zoom

Or NPM:

npm install angular-pan-zoom --save

Features:

  • Zoom using mouse wheel, double click, or control widget
  • Pan using click and drag. When releasing mouse button while panning, the pan will come to a gradual stop.
  • AngularJS integrated. Models are used as APIs for communicating
  • Widget with zoom controls provided. Use this or design your own controls if you so prefer.

Requirements:

  • AngularJS (obviously)
  • jQuery (used for managing timing loops)
  • Hamster.JS (for mouse wheel support)
  • angular-mousewheel (which integrates HamsterJS with angular)

For convenience, these requirements are checked in as part of the project. They are also described by the bower.json file.

Demo:

Click here for an online demo of the functionality.

Usage:

Simplest working example

When declaring your module:

angular.module('your_module', ['panzoom', ..])

In your controller:

$scope.config = {}; // use defaults
$scope.model = {}; // always pass empty object

In your markup:

<body>
  ..
  <!-- create panzoom, passing models from controller -->
  <panzoom config="config" model="model" style="width:800px; height: 600px">
    <!-- your content here -->
  </panzoom>
  ..
  <!-- include scripts -->
  <script src="bower_components/jQuery/dist/jQuery.js"></script>
  <script src="bower_components/hamsterjs/hamster.js"></script>
  <script src="bower_components/angular/angular.min.js"></script>
  <script src="bower_components/angular-mousewheel/mousewheel.js"></script>
  <script src="release/panzoom.js"></script>
</body>

This will provide zoom and pan functionality using default settings. It will, however, not provide any widget to control the zoom and pan.

Using the provided zoom pan widget

To use the bundled <panzoomwidget>, you need to

  • include release/panzoomwidget.css on your page
  • make your AngularJS module depend upon the panzoomwidget module
  • declare an id attribute on your <panzoom> tag
  • use a <panzoomwidget> directive in the markup, specifying its panzoom-id attribute to be the same value as the id of the <panzoom> tag You will probably also want to position the widget above the zoomed contents be means of CSS. Check ./test.html for a working example.

Implementing your own external controls

Refer to panzoomwidget.js for an example of how this may be done. Whether or not you create it as a directive is up to you. To access the API of a panzoom directive, you need to look it up using the getAPI() method on the bundled PanZoomService, passing the id of the <panzoom> widget. The method will return a promise.

Example usage:

// assuming the PanZoomService to be a dependency and panzoomId to be the ID of the <panzoom> directive ...
PanZoomService.getAPI(panzoomId).then(function (api) {
    // you can now invoke the api
}

The API

The API object contains the following properties:

MethodDescription
modelthe model object which was passed to the panzoom directive
configthe config object which was passed to the panzoom directive
changeZoomLevel(newZoomLevel [,clickPoint])change zoom level to a new value using a quick animation
zoomIn()shorthand for increasing zoom level by one
zoomOut()shorthand for decreasing zoom level by one
zoomToFit(rectangle)zoom to display a part of the contents. Example rectangle: { "x": 0, "y": 100, "width": 100, "height": 100 }
getViewPosition(modelPosition)takes a argument a {x:.., y:..} in the original, untransformed contents. Returns the current pixel position of this point.
getModelPosition(viewPosition)the reverse operation of getViewPosition()

The config object:

May be used to pass configuration options to the panzoom directive. The directive will fill in any "blanks" with default values.

The config object is not intended to be modified once initialized.

The following config object attributes are supported:

NameTypeDefaultDescription
zoomLevelsnumber5Number of discrete zoom levels, each one representing a scale.
neutralZoomLevelnumber2The zoom level at which the centents render at 1:1 scale
scalePerZoomLevelnumber2.0The difference in actual scale between two adjacent zoom levels.
initialZoomLevelnumberneutralZoomLevelThe initially selected zoom level
initialPanXnumber0The initial pan in the horizontal direction
initialPanYnumber0The initial pan in the vertical direction
zoomToFitZoomLevelFactornumber0.95A number to indicate how closely zoom to fit will work. 1.0 is perfect fit, lowering the number will reveal a bit of the surrounding contents
zoomOnDoubleClickbooleantrueEnable or disable zoom in on double click
zoomButtonIncrementnumber1.0The amount of zoom levels to zoom on double click
zoomStepDurationnumber0.2Amount of seconds to animate between two adjacent zoom levels
disableZoomAnimationbooleanfalseSet to true to disable the animation while zooming. It will be more chunky but will consule less CPU resources.
zoomOnMouseWheelbooleantrueEnable or disable zoom in/out on mouse wheel
invertMouseWheelbooleanfalseInvert the behaviour of the mouse wheel (or two finger trackpad gesture)
frictionnumber10.0Constant which controls the friction when dragging and then letting go. The higher the number, the more quickly the animation will come to a stop.
haltSpeednumber100.0Constant which controls when the pan animation has slowed down enough to be terminated. The lower the number, the longer time it will run.
panOnClickDragbooleantrueEnable or disable pan on clicking and dragging the mouse
modelChangedCallbackfunctionundefinedPass a function to receive events when the model changes. The model will be passed to the function.
useHardwareAccelerationbooleanfalseUse translate3d for panning instead of using standard CSS styles 'left' and 'top'. This is intended to trigger hardware acceleration and may increase the speed greatly. In future versions, this may be set to true as default.
chromeUseTransformbooleanfalseCause Chrome to use CSS transform instead of CSS zoom. Enable if you use nested SVG and see performance problems in Chrome.
initialZoomToFitrectangleundefinedWhen defined, will initially zoom to fit the given rectangle (see API for explanation of zoom to fit). This overrides the initialZoomLevel, initialPanX, and initialPanY values.
keepInBoundsbooleanfalseWhen true, it will not be possible to pan the contents off the screen -- it will snap back when trying to do so -- and it will not be possible to zoom further out than the neutral zoom level.
keepInBoundsRestoreForcenumber0.5Constant to control how quickly the contents snap back in place after attempting to pan off bounds.
keepInBoundsDragPullbacknumber0.7Constant to control the perceived force preventing dragging the contents off limits.

The model object:

When initializing, you should pass an empty object. The directive will initialize the object. You can read the current zoom and pan state at any time from this object.

Contributing to the project:

Any code contributions to the project will be appreciated. A few guidelines follow.

Npm is used for building stuff. Use npm install to fetch dependencies (including the bower ones). Use npm start to launch a development server using browsersync. Use 'npm run build' to perform a build as verified by uni tests and lint. For the complete list of npm scripts, see package.json.

1.0.18

8 years ago

1.0.17

8 years ago

1.0.16

8 years ago

1.0.15

8 years ago

1.0.14

9 years ago

1.0.13

9 years ago

1.0.12

9 years ago