Skip to content

Commit

Permalink
Add zoomBy to the public API
Browse files Browse the repository at this point in the history
  • Loading branch information
charlespwd committed Aug 11, 2016
1 parent 2815073 commit 87a7c83
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
10 changes: 9 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export default function PublicWorldView(render, opts) {
return {
setContainerOrigin: view.setContainerOrigin,
zoomAtMouse,
zoomBy,
panStart,
panMove,
panEnd,
Expand All @@ -48,7 +49,14 @@ export default function PublicWorldView(render, opts) {
}

function zoomAtMouse(wheelDelta, e = { pageX: undefined, pageY: undefined }) {
const change = wheelDelta > 0 ? 0.03 : -0.03 // %
const change = wheelDelta > 0 ? 1.03 : 0.97 // %
const pointer_document = typeof e.pageX === 'number' ? fromEventToVector(e) : undefined
view.zoomBy(change, pointer_document)
publish()
}

// Where change is the ratio between the previous and the future scale level
function zoomBy(change, e = { pageX: undefined, pageY: undefined }) {
const pointer_document = typeof e.pageX === 'number' ? fromEventToVector(e) : undefined
view.zoomBy(change, pointer_document)
publish()
Expand Down
29 changes: 26 additions & 3 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,40 @@ describe('Module: PublicApi', () => {

it('should zoom positively by 3% when the wheel delta is positive', () => {
view.zoomAtMouse(10)
expect(wvMock.zoomBy).to.have.been.calledWith(0.03)
expect(wvMock.zoomBy).to.have.been.calledWith(1.03)
})

it('should zoom negatively by 3% when the wheel delta is negative', () => {
view.zoomAtMouse(-10)
expect(wvMock.zoomBy).to.have.been.calledWith(-0.03)
expect(wvMock.zoomBy).to.have.been.calledWith(0.97)
})

it('should turn the event position into a vector', () => {
view.zoomAtMouse(-10, pointer(10, 10))
expect(wvMock.zoomBy).to.have.been.calledWith(-0.03, [10, 10])
expect(wvMock.zoomBy).to.have.been.calledWith(0.97, [10, 10])
})
})

describe('Unit: zoomBy', () => {
beforeEach(() => {
wvMock.zoomBy = sinon.spy()
})

it('should publish', () => {
view.zoomBy(10)
expect(renderSpy).to.have.been.called
})

it('should zoom positively by change', () => {
view.zoomBy(10)
expect(wvMock.zoomBy).to.have.been.calledWith(10)
view.zoomBy(0.01)
expect(wvMock.zoomBy).to.have.been.calledWith(0.01)
})

it('should turn the event position into a vector', () => {
view.zoomBy(10, pointer(10, 10))
expect(wvMock.zoomBy).to.have.been.calledWith(10, [10, 10])
})
})
})

0 comments on commit 87a7c83

Please sign in to comment.