Skip to content

Commit

Permalink
setup the frame
Browse files Browse the repository at this point in the history
  • Loading branch information
jarvisniu committed Feb 26, 2019
1 parent fb7b235 commit ea7bc85
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.DS_Store

.cache/
dist/
node_modules/

yarn.lock
package-lock.json
28 changes: 28 additions & 0 deletions demo/01.basic/app.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<template>
<div class="app">
<!-- <div>App</div> -->
<vue-zoomer class="zoomer">
<img src="../assets/landscape-1.jpg" class="image">
</vue-zoomer>
</div>
</template>

<style lang="stylus">
body
margin 0
</style>

<style lang="stylus" scoped>
.app
width 100vw
height 100vh
.zoomer
width 100%
height: 100%
.image
width 100%
height 100%
object-fit contain
</style>
13 changes: 13 additions & 0 deletions demo/01.basic/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>01.Basic - Vue Zoomer Demo</title>
</head>
<body>
<div id="app"></div>
<script src="./main.js"></script>
</body>
</html>
12 changes: 12 additions & 0 deletions demo/01.basic/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import Vue from 'vue'

import VueZoomer from '../../src/vue-zoomer.vue'
import App from './app.vue'

Vue.component('VueZoomer', VueZoomer)
Vue.component('App', App)

new Vue({
el: '#app',
render: h => h('App'),
})
Binary file added demo/assets/landscape-1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/assets/landscape-2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/assets/landscape-3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/assets/landscape-4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added demo/assets/landscape-5.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "vue-zoomer",
"version": "0.1.0",
"main": "index.js",
"scripts": {
"dev": "parcel demo/01.basic/index.html --port 5100"
},
"repository": "git@github.com:jarvisniu/vue-zoomer.git",
"author": "Jarvis Niu <jarvisniu@gmail.com>",
"license": "MIT",
"dependencies": {
"app.vue": "^0.1.10",
"parcel-bundler": "^1.11.0",
"stylus": "^0.54.5",
"vue": "^2.6.7",
"vue-hot-reload-api": "^2.3.3"
},
"devDependencies": {
"@vue/component-compiler-utils": "^2.6.0",
"vue-template-compiler": "^2.6.7"
}
}
67 changes: 67 additions & 0 deletions src/vue-zoomer.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<template>
<div class="vue-zoomer">
<div class="zoomer" :style="wrapperStyle">
<!-- <div>Vue Zoomer</div> -->
<slot/>
</div>
</template>

<script>
export default {
data () {
return {
// 容器宽高,用来决定缩放器的宽高。需要响应缩放和旋转。
containerWidth: 1,
containerHeight: 1,
// 中心值:交互最终变更这些值,旋转缩放后该值维持不变;
translateX: 0,
translateY: 0,
scale: 0.5, // 相对容器的
}
},
mounted () {
console.log('container Width/Height: ', this.containerWidth, this.containerHeight)
window.addEventListener('resize', this.onResize)
this.onResize()
},
destroyed () {
window.removeEventListener('resize', this.onResize)
},
computed: {
wrapperStyle () {
let W = this.containerWidth
let H = this.containerHeight
let width = W * this.scale
let height = H * this.scale
let left = W * this.translateX + W * (1 - this.scale) / 2
let top = H * this.translateY + H * (1 - this.scale) / 2
return {
left: `${ left }px`,
top: `${ top }px`,
width: `${ width }px`,
height: `${ height }px`,
}
},
},
methods: {
onResize () {
let styles = window.getComputedStyle(this.$el)
this.containerWidth = parseFloat(styles.width)
this.containerHeight = parseFloat(styles.height)
},
},
}
</script>

<style lang="stylus" scoped>
.vue-zoomer
position relative
overflow hidden
background-color hsla(0, 0%, 0%, 0.5)
.zoomer
position absolute
& > img
// remove the 4px gap below the image
vertical-align top
</style>

0 comments on commit ea7bc85

Please sign in to comment.