|
| 1 | +<script> |
| 2 | +import { BoundingBox, ResizeEvent } from 'vue-advanced-cropper'; |
| 3 | +import {resize} from 'vue-advanced-cropper/src/core/algorithms' |
| 4 | +
|
| 5 | +export default { |
| 6 | + name: 'AlgorithmTester', |
| 7 | + components: { |
| 8 | + BoundingBox, |
| 9 | + }, |
| 10 | + data() { |
| 11 | + return { |
| 12 | + coordinates: { |
| 13 | + cropper: { |
| 14 | + width: 100, |
| 15 | + height: 100, |
| 16 | + left: 150, |
| 17 | + top: 150, |
| 18 | + }, |
| 19 | + stencil: { |
| 20 | + width: 100, |
| 21 | + height: 100, |
| 22 | + left: 150, |
| 23 | + top: 150, |
| 24 | + }, |
| 25 | + current: { |
| 26 | + width: 100, |
| 27 | + height: 100, |
| 28 | + left: 150, |
| 29 | + top: 150, |
| 30 | + }, |
| 31 | + area: { |
| 32 | + width: 400, |
| 33 | + height: 400, |
| 34 | + left: 0, |
| 35 | + top: 0, |
| 36 | + }, |
| 37 | + }, |
| 38 | + stencilProps: `{\n\t"aspectRatio": 10/16\n}`, |
| 39 | + }; |
| 40 | + }, |
| 41 | + computed: { |
| 42 | + cropperStyle() { |
| 43 | + return { |
| 44 | + width: `${this.coordinates.cropper.width}px`, |
| 45 | + height: `${this.coordinates.cropper.height}px`, |
| 46 | + top: `${this.coordinates.cropper.top}px`, |
| 47 | + left: `${this.coordinates.cropper.left}px`, |
| 48 | + } |
| 49 | + }, |
| 50 | + currentStyle() { |
| 51 | + return { |
| 52 | + width: `${this.coordinates.current.width}px`, |
| 53 | + height: `${this.coordinates.current.height}px`, |
| 54 | + top: `${this.coordinates.current.top}px`, |
| 55 | + left: `${this.coordinates.current.left}px`, |
| 56 | + } |
| 57 | + }, |
| 58 | + stencilStyle() { |
| 59 | + return { |
| 60 | + width: `${this.coordinates.stencil.width}px`, |
| 61 | + height: `${this.coordinates.stencil.height}px`, |
| 62 | + top: `${this.coordinates.stencil.top}px`, |
| 63 | + left: `${this.coordinates.stencil.left}px`, |
| 64 | + } |
| 65 | + }, |
| 66 | + areaStyle() { |
| 67 | + return { |
| 68 | + width: `${this.coordinates.area.width}px`, |
| 69 | + height: `${this.coordinates.area.height}px`, |
| 70 | + top: `${this.coordinates.area.top}px`, |
| 71 | + left: `${this.coordinates.area.left}px`, |
| 72 | + } |
| 73 | + } |
| 74 | + }, |
| 75 | + methods: { |
| 76 | + onResizeStencil({directions}) { |
| 77 | + this.coordinates.stencil.left -= directions.left |
| 78 | + this.coordinates.stencil.top -= directions.top |
| 79 | + this.coordinates.stencil.width += directions.right + directions.left |
| 80 | + this.coordinates.stencil.height += directions.top + directions.bottom |
| 81 | + }, |
| 82 | + onResizeArea({directions}) { |
| 83 | + this.coordinates.area.left -= directions.left |
| 84 | + this.coordinates.area.top -= directions.top |
| 85 | + this.coordinates.area.width += directions.right + directions.left |
| 86 | + this.coordinates.area.height += directions.top + directions.bottom |
| 87 | + }, |
| 88 | + onResizeCurrent({directions}) { |
| 89 | + this.coordinates.current.left -= directions.left |
| 90 | + this.coordinates.current.top -= directions.top |
| 91 | + this.coordinates.current.width += directions.right + directions.left |
| 92 | + this.coordinates.current.height += directions.top + directions.bottom |
| 93 | + }, |
| 94 | + onResize(complete) { |
| 95 | + const coordinates = resize({ |
| 96 | + coordinates: {...this.coordinates.current}, |
| 97 | + restrictions: { |
| 98 | + minWidth: 100, |
| 99 | + minHeight: 100, |
| 100 | + maxWidth: 400, |
| 101 | + maxHeight: 400, |
| 102 | + }, |
| 103 | + aspectRatio: { |
| 104 | + minimum: 10/20, |
| 105 | + }, |
| 106 | + allowedArea: { |
| 107 | + left: this.coordinates.area.left, |
| 108 | + top: this.coordinates.area.top, |
| 109 | + bottom: this.coordinates.area.top + this.coordinates.area.height, |
| 110 | + right: this.coordinates.area.left + this.coordinates.area.width, |
| 111 | + }, |
| 112 | + coefficient: 1, |
| 113 | + resizeEvent: new ResizeEvent({}, { |
| 114 | + left: -(this.coordinates.stencil.left - this.coordinates.current.left), |
| 115 | + top: -(this.coordinates.stencil.top - this.coordinates.current.top), |
| 116 | + bottom: this.coordinates.stencil.top + this.coordinates.stencil.height - (this.coordinates.current.top + this.coordinates.current.height), |
| 117 | + right: this.coordinates.stencil.left + this.coordinates.stencil.width - (this.coordinates.current.left + this.coordinates.current.width), |
| 118 | + }, { |
| 119 | + compensate: true |
| 120 | + }), |
| 121 | + complete |
| 122 | + }) |
| 123 | + this.coordinates.current = {...coordinates} |
| 124 | +
|
| 125 | + } |
| 126 | + } |
| 127 | +}; |
| 128 | +</script> |
| 129 | + |
| 130 | +<template> |
| 131 | + <div class="bounding-box-example"> |
| 132 | + <div class="playground"> |
| 133 | + <div class="area-wrapper"> |
| 134 | + <div class="area"> |
| 135 | + <BoundingBox |
| 136 | + class="area-box" |
| 137 | + handler-component="simple-handler" |
| 138 | + line-component="simple-line" |
| 139 | + :style="areaStyle" |
| 140 | + :lines-classnames="{default: 'line line--area'}" |
| 141 | + :handlers-classnames="{default: 'handler handler--area'}" |
| 142 | + @resize="onResizeArea" |
| 143 | + /> |
| 144 | + <BoundingBox |
| 145 | + class="stencil-box" |
| 146 | + handler-component="simple-handler" |
| 147 | + line-component="simple-line" |
| 148 | + :style="stencilStyle" |
| 149 | + :lines-classnames="{default: 'line line--stencil'}" |
| 150 | + :handlers-classnames="{default: 'handler handler--stencil'}" |
| 151 | + @resize="onResizeStencil" |
| 152 | + /> |
| 153 | + <div |
| 154 | + class="current-box" |
| 155 | + :style="currentStyle" |
| 156 | + /> |
| 157 | + </div> |
| 158 | + <div class="buttons"> |
| 159 | + <div class="button" @click="onResize(false)"> |
| 160 | + <img :src="require('../assets/icons/resize.svg')"/> |
| 161 | + </div> |
| 162 | + <div class="button button--complete" @click="onResize(true)"> |
| 163 | + <img :src="require('../assets/icons/resize.svg')"/> |
| 164 | + </div> |
| 165 | + </div> |
| 166 | + |
| 167 | + </div> |
| 168 | + </div> |
| 169 | + <div class="options"> |
| 170 | + <div class="option"> |
| 171 | + Stencil props: |
| 172 | + <textarea class="option__textarea" v-model="stencilProps"/> |
| 173 | + </div> |
| 174 | + </div> |
| 175 | + </div> |
| 176 | +</template> |
| 177 | + |
| 178 | +<style lang="scss"> |
| 179 | + .bounding-box-example { |
| 180 | + position: relative; |
| 181 | + padding-top: 20px; |
| 182 | + .playground { |
| 183 | + position: relative; |
| 184 | + } |
| 185 | + .buttons { |
| 186 | + position: absolute; |
| 187 | + right: 20px; |
| 188 | + bottom: 20px; |
| 189 | + } |
| 190 | + .button { |
| 191 | + background: rgb(61, 82, 206); |
| 192 | + border-radius: 50%; |
| 193 | + height: 50px; |
| 194 | + width: 50px; |
| 195 | + margin-top: 10px; |
| 196 | + display: flex; |
| 197 | + align-items: center; |
| 198 | + justify-content: center; |
| 199 | + cursor: pointer; |
| 200 | + &--complete { |
| 201 | + background: #3fb37f; |
| 202 | + } |
| 203 | + } |
| 204 | + .options { |
| 205 | + border-top: solid 1px #EEE; |
| 206 | + padding-top: 20px; |
| 207 | + } |
| 208 | + .option { |
| 209 | + &__textarea { |
| 210 | + display: block; |
| 211 | + height: 150px; |
| 212 | + width: 100%; |
| 213 | + } |
| 214 | + } |
| 215 | + .area-wrapper { |
| 216 | + display: flex; |
| 217 | + align-items: center; |
| 218 | + justify-content: center; |
| 219 | + padding-top: 50px; |
| 220 | + padding-bottom: 50px; |
| 221 | + overflow: hidden; |
| 222 | + } |
| 223 | + .area { |
| 224 | + width: 400px; |
| 225 | + height: 400px; |
| 226 | + position: relative; |
| 227 | + outline: dashed 1px #EEE; |
| 228 | + } |
| 229 | + .current-box { |
| 230 | + border: solid 1px rgba(255,150,150, 0.8); |
| 231 | + } |
| 232 | + .stencil-box, .area-box, .current-box { |
| 233 | + pointer-events: none; |
| 234 | + position: absolute; |
| 235 | + } |
| 236 | + .line { |
| 237 | + pointer-events: all; |
| 238 | + &--stencil { |
| 239 | + border-color: rgba(#3fb37f, 0.5); |
| 240 | + } |
| 241 | + &--area { |
| 242 | + border-color: rgba(#555, 0.5); |
| 243 | + border-style: dotted; |
| 244 | + } |
| 245 | + } |
| 246 | + .handler { |
| 247 | + pointer-events: all; |
| 248 | + &--stencil { |
| 249 | + background: #3fb37f; |
| 250 | + } |
| 251 | + &--area { |
| 252 | + background: #555; |
| 253 | + opacity: 0.5; |
| 254 | + } |
| 255 | + } |
| 256 | + } |
| 257 | +</style> |
0 commit comments