Last active
March 29, 2019 17:16
-
-
Save ArnaudValensi/a94d0226dc12c7b6cf022ec55d330723 to your computer and use it in GitHub Desktop.
New Twiddle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
let previousMouseX = 0; | |
let previousMouseY = 0; | |
let mouseXInImage = 0; | |
let mouseYInImage = 0; | |
let scale = 1; | |
const zoomSpeed = 0.1; | |
export default Ember.Component.extend({ | |
didInsertElement() { | |
this._super(...arguments); | |
console.log('didInsertElement'); | |
$('.zoomable-area').on('wheel', (event) => { | |
this.pinch(event); | |
}); | |
}, | |
pinch(event) { | |
console.time('test'); | |
const $zoomableArea = this.$('.zoomable-area'); | |
const $image = this.$('.image'); | |
console.timeEnd('test') | |
const wheelDeltaY = event.originalEvent.deltaY; | |
// Get mouse position relative to the wrapper. | |
const mouseX = event.pageX - $zoomableArea.offset().left; | |
const mouseY = event.pageY - $zoomableArea.offset().top; | |
console.log(`mouseX: ${mouseX}, mouseY: ${mouseY}`); | |
// Find current location on the image at the current scale. | |
mouseXInImage = mouseXInImage + ((mouseX - previousMouseX) / scale); | |
mouseYInImage = mouseYInImage + ((mouseY - previousMouseY) / scale); | |
// Update the scale. | |
scale += wheelDeltaY * zoomSpeed; | |
scale = scale < 1 ? 1 : (scale > 64 ? 64 : scale); | |
// Determine the location in the wrapper at the new scale and relative to | |
// the cursor location in the image. | |
const imageX = (mouseX - mouseXInImage) / scale; | |
const imageY = (mouseY - mouseYInImage) / scale; | |
// Save the current mouse location location relative to the wrapper. | |
previousMouseX = mouseX; | |
previousMouseY = mouseY; | |
const transform = `scale(${scale}) translate(${imageX}px, ${imageY}px)`; | |
console.log(`transform: ${transform}`); | |
$image.css('transform', transform); | |
const transformOrigin = `${mouseXInImage}px ${mouseYInImage}px`; | |
console.log(`transformOrigin: ${transformOrigin}`); | |
$image.css('transform-origin', transformOrigin); | |
}, | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle' | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
*, *:before, *:after { | |
box-sizing: border-box; | |
} | |
body { | |
margin: 12px 16px; | |
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; | |
font-size: 12pt; | |
} | |
.zoomable-area { | |
border: dashed 1px; | |
width: 300px; | |
height: 300px; | |
overflow: hidden; | |
} | |
.image { | |
user-drag: none; | |
user-select: none; | |
-moz-user-select: none; | |
-webkit-user-drag: none; | |
-webkit-user-select: none; | |
-ms-user-select: none; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.15.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js", | |
"ember": "3.4.3", | |
"ember-template-compiler": "3.4.3", | |
"ember-testing": "3.4.3" | |
}, | |
"addons": { | |
"ember-data": "3.4.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment