index.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>test vue-cropper</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. }
  13. .cut {
  14. width: 500px;
  15. height: 500px;
  16. margin: 30px auto;
  17. }
  18. .c-item {
  19. max-width: 800px;
  20. margin: 10px auto;
  21. margin-top: 20px;
  22. }
  23. .content {
  24. margin: auto;
  25. max-width: 1200px;
  26. margin-bottom: 100px;
  27. }
  28. .test-button {
  29. display: flex;
  30. flex-wrap: wrap;
  31. align-content: center;
  32. justify-content: center;
  33. }
  34. .btn {
  35. display: inline-block;
  36. line-height: 1;
  37. white-space: nowrap;
  38. cursor: pointer;
  39. background: #fff;
  40. border: 1px solid #c0ccda;
  41. color: #1f2d3d;
  42. text-align: center;
  43. box-sizing: border-box;
  44. outline: none;
  45. margin:20px 10px 0px 0px;
  46. padding: 9px 15px;
  47. font-size: 14px;
  48. border-radius: 4px;
  49. color: #fff;
  50. background-color: #50bfff;
  51. border-color: #50bfff;
  52. transition: all .2s ease;
  53. text-decoration: none;
  54. user-select: none;
  55. }
  56. .des {
  57. line-height: 30px;
  58. }
  59. code.language-html {
  60. padding: 10px 20px;
  61. margin: 10px 0px;
  62. display: block;
  63. background-color: #333;
  64. color: #fff;
  65. overflow-x: auto;
  66. font-family: Consolas, Monaco, Droid, Sans, Mono, Source, Code, Pro, Menlo, Lucida, Sans, Type, Writer, Ubuntu, Mono;
  67. border-radius: 5px;
  68. white-space: pre;
  69. }
  70. .show-info {
  71. margin-bottom: 50px;
  72. }
  73. .show-info h2 {
  74. line-height: 50px;
  75. }
  76. /*.title, .title:hover, .title-focus, .title:visited {
  77. color: black;
  78. }*/
  79. .title {
  80. display: block;
  81. text-decoration: none;
  82. text-align: center;
  83. line-height: 1.5;
  84. margin: 20px 0px;
  85. background-image: -webkit-linear-gradient(left,#3498db,#f47920 10%,#d71345 20%,#f7acbc 30%,#ffd400 40%,#3498db 50%,#f47920 60%,#d71345 70%,#f7acbc 80%,#ffd400 90%,#3498db);
  86. color: transparent;
  87. -webkit-background-clip: text;
  88. background-size: 200% 100%;
  89. animation: slide 5s infinite linear;
  90. font-size: 40px;
  91. }
  92. .test {
  93. height: 500px;
  94. }
  95. .model {
  96. position: fixed;
  97. z-index: 10;
  98. width: 100vw;
  99. height: 100vh;
  100. overflow: auto;
  101. top: 0;
  102. left: 0;
  103. background: rgba(0, 0, 0, 0.8);
  104. }
  105. .model-show {
  106. display: flex;
  107. justify-content: center;
  108. align-items: center;
  109. width: 100vw;
  110. height: 100vh;
  111. }
  112. .model img {
  113. display: block;
  114. margin: auto;
  115. max-width: 80%;
  116. user-select: none;
  117. background-position: 0px 0px, 10px 10px;
  118. background-size: 20px 20px;
  119. background-image: linear-gradient(45deg, #eee 25%, transparent 25%, transparent 75%, #eee 75%, #eee 100%),linear-gradient(45deg, #eee 25%, white 25%, white 75%, #eee 75%, #eee 100%);
  120. }
  121. .c-item {
  122. display: block;
  123. user-select: none;
  124. }
  125. @keyframes slide {
  126. 0% {
  127. background-position: 0 0;
  128. }
  129. 100% {
  130. background-position: -100% 0;
  131. }
  132. }
  133. </style>
  134. </head>
  135. <body>
  136. <div id="app">
  137. <div class="model" v-show="model" @click="model = false">
  138. <div class="model-show">
  139. <img :src="modelSrc" alt="">
  140. </div>
  141. </div>
  142. <p>例子</p>
  143. <div class="cut">
  144. <vue-cropper ref="cropper" :img="option.img" :output-size="option.size" :output-type="option.outputType" :info="true" :full="option.full"
  145. :can-move="option.canMove" :can-move-box="option.canMoveBox" :fixed-box="option.fixedBox" :original="option.original"
  146. :auto-crop="option.autoCrop" :auto-crop-width="option.autoCropWidth" :auto-crop-height="option.autoCropHeight" :center-box="option.centerBox"
  147. @real-time="realTime" :high="option.high" ></vue-cropper>
  148. </div>
  149. <div class="test-button">
  150. <button @click="changeImg" class="btn">changeImg</button>
  151. <label class="btn" for="uploads">upload</label>
  152. <input type="file" id="uploads" style="position:absolute; clip:rect(0 0 0 0);" accept="image/png, image/jpeg, image/gif, image/jpg"
  153. @change="uploadImg($event, 1)">
  154. <button @click="startCrop" v-if="!crap" class="btn">start</button>
  155. <button @click="stopCrop" v-else class="btn">stop</button>
  156. <button @click="clearCrop" class="btn">clear</button>
  157. <button @click="refreshCrop" class="btn">refresh</button>
  158. <button @click="changeScale(1)" class="btn">+</button>
  159. <button @click="changeScale(-1)" class="btn">-</button>
  160. <button @click="rotateLeft" class="btn">rotateLeft</button>
  161. <button @click="rotateRight" class="btn">rotateRight</button>
  162. <button @click="finish('base64')" class="btn">preview(base64)</button>
  163. <button @click="finish('blob')" class="btn">preview(blob)</button>
  164. <a @click="down('base64')" class="btn">download(base64)</a>
  165. <a @click="down('blob')" class="btn">download(blob)</a>
  166. <div style="display:block; width: 100%;">
  167. <label class="c-item">
  168. <span>上传图片是否显示原始宽高 (针对大图 可以铺满)</span>
  169. <input type="checkbox" v-model="option.original">
  170. <span>original: {{ option.original}}</span>
  171. </label>
  172. <label class="c-item">
  173. <span>能否拖动图片</span>
  174. <input type="checkbox" v-model="option.canMove">
  175. </label>
  176. <label class="c-item">
  177. <span>能否拖动截图框</span>
  178. <input type="checkbox" v-model="option.canMoveBox">
  179. <span>canMoveBox: {{ option.canMoveBox}}</span>
  180. </label>
  181. <label class="c-item">
  182. <span>截图框固定大小</span>
  183. <input type="checkbox" v-model="option.fixedBox">
  184. <span>fixedBox: {{ option.fixedBox}}</span>
  185. </label>
  186. <label class="c-item">
  187. <span>是否输出原图比例的截图</span>
  188. <input type="checkbox" v-model="option.full">
  189. <span>full: {{ option.full}}</span>
  190. </label>
  191. <label class="c-item">
  192. <span>是否自动生成截图框</span>
  193. <input type="checkbox" v-model="option.autoCrop">
  194. <span>autoCrop: {{ option.autoCrop}}</span>
  195. </label>
  196. <label class="c-item">
  197. <span>是否根据dpr生成适合屏幕的高清图片</span>
  198. <input type="checkbox" v-model="option.high">
  199. <span>high: {{ option.high}}</span>
  200. </label>
  201. <label class="c-item">
  202. <span>截图框是否限制在图片里(只有在自动生成截图框时才能生效)</span>
  203. <input type="checkbox" v-model="option.centerBox">
  204. <span>centerBox: {{ option.centerBox}}</span>
  205. </label>
  206. <label class="c-item">
  207. <p>输出图片格式</p>
  208. <label>jpg
  209. <input type="radio" name="type" value="jpeg" v-model="option.outputType">
  210. </label>
  211. <label>png
  212. <input type="radio" name="type" value="png" v-model="option.outputType">
  213. </label>
  214. <label>webp
  215. <input type="radio" name="type" value="webp" v-model="option.outputType">
  216. </label>
  217. </label>
  218. </div>
  219. </div>
  220. <div class="show-preview" :style="{'width': previews.w + 'px', 'height': previews.h + 'px', 'overflow': 'hidden', 'margin': '5px'}">
  221. <div :style="previews.div">
  222. <img :src="previews.url" :style="previews.img">
  223. </div>
  224. </div>
  225. </div>
  226. <!-- <script src="https://vuejs.org/js/vue.min.js"></script> -->
  227. <script src="https://cdn.jsdelivr.net/npm/vue@2.5.17/dist/vue.js"></script>
  228. <script src="../dist/index.js"></script>
  229. <script>
  230. Vue.use(window['vue-cropper'])
  231. var a = new Vue({
  232. el: '#app',
  233. data: {
  234. model: false,
  235. modelSrc: '',
  236. crap: false,
  237. previews: {},
  238. lists: [
  239. {
  240. img: 'http://ofyaji162.bkt.clouddn.com/touxiang.jpg'
  241. },
  242. {
  243. img: 'https://o90cnn3g2.qnssl.com/0C3ABE8D05322EAC3120DDB11F9D1F72.png'
  244. },
  245. {
  246. img: 'http://ofyaji162.bkt.clouddn.com/bg1.jpg',
  247. },
  248. {
  249. img: 'http://ofyaji162.bkt.clouddn.com/bg2.jpg',
  250. },
  251. {
  252. img: 'http://ofyaji162.bkt.clouddn.com/can.jpg'
  253. },
  254. {
  255. img: 'http://ofyaji162.bkt.clouddn.com/nightcat.jpg'
  256. }
  257. ],
  258. option: {
  259. img: 'https://avatars3.githubusercontent.com/u/15681693',
  260. size: 1,
  261. full: false,
  262. outputType: 'png',
  263. canMove: true,
  264. fixedBox: false,
  265. original: false,
  266. canMoveBox: true,
  267. autoCrop: true,
  268. // 只有自动截图开启 宽度高度才生效
  269. autoCropWidth: 200,
  270. autoCropHeight: 150,
  271. centerBox: false,
  272. high: true
  273. },
  274. show: true
  275. },
  276. methods: {
  277. changeImg() {
  278. this.option.img = this.lists[~~(Math.random() * this.lists.length)].img
  279. },
  280. startCrop() {
  281. // start
  282. this.crap = true
  283. this.$refs.cropper.startCrop()
  284. },
  285. stopCrop() {
  286. // stop
  287. this.crap = false
  288. this.$refs.cropper.stopCrop()
  289. },
  290. clearCrop() {
  291. // clear
  292. this.$refs.cropper.clearCrop()
  293. },
  294. refreshCrop() {
  295. // clear
  296. this.$refs.cropper.refresh()
  297. },
  298. changeScale(num) {
  299. num = num || 1
  300. this.$refs.cropper.changeScale(num)
  301. },
  302. rotateLeft() {
  303. this.$refs.cropper.rotateLeft()
  304. },
  305. rotateRight() {
  306. this.$refs.cropper.rotateRight()
  307. },
  308. finish(type) {
  309. // 输出
  310. // var test = window.open('about:blank')
  311. // test.document.body.innerHTML = '图片生成中..'
  312. if (type === 'blob') {
  313. this.$refs.cropper.getCropBlob((data) => {
  314. var img = window.URL.createObjectURL(data)
  315. this.model = true
  316. this.modelSrc = img
  317. })
  318. } else {
  319. this.$refs.cropper.getCropData((data) => {
  320. this.model = true
  321. this.modelSrc = data
  322. })
  323. }
  324. },
  325. // 实时预览函数
  326. realTime(data) {
  327. console.log(data)
  328. this.previews = data
  329. },
  330. finish2(type) {
  331. this.$refs.cropper2.getCropData((data) => {
  332. this.model = true
  333. this.modelSrc = data
  334. })
  335. },
  336. finish3(type) {
  337. this.$refs.cropper3.getCropData((data) => {
  338. this.model = true
  339. this.modelSrc = data
  340. })
  341. },
  342. down(type) {
  343. // event.preventDefault()
  344. var aLink = document.createElement('a')
  345. aLink.download = 'demo'
  346. // 输出
  347. if (type === 'blob') {
  348. this.$refs.cropper.getCropBlob((data) => {
  349. this.downImg = window.URL.createObjectURL(data)
  350. aLink.href = window.URL.createObjectURL(data)
  351. aLink.click()
  352. })
  353. } else {
  354. this.$refs.cropper.getCropData((data) => {
  355. this.downImg = data
  356. aLink.href = data
  357. aLink.click()
  358. })
  359. }
  360. },
  361. uploadImg(e, num) {
  362. //上传图片
  363. // this.option.img
  364. var file = e.target.files[0]
  365. if (!/\.(gif|jpg|jpeg|png|bmp|GIF|JPG|PNG)$/.test(e.target.value)) {
  366. alert('图片类型必须是.gif,jpeg,jpg,png,bmp中的一种')
  367. return false
  368. }
  369. var reader = new FileReader()
  370. reader.onload = (e) => {
  371. let data
  372. if (typeof e.target.result === 'object') {
  373. // 把Array Buffer转化为blob 如果是base64不需要
  374. data = window.URL.createObjectURL(new Blob([e.target.result]))
  375. } else {
  376. data = e.target.result
  377. }
  378. if (num === 1) {
  379. this.option.img = data
  380. } else if (num === 2) {
  381. this.example2.img = data
  382. }
  383. }
  384. // 转化为base64
  385. // reader.readAsDataURL(file)
  386. // 转化为blob
  387. reader.readAsArrayBuffer(file)
  388. },
  389. imgLoad(msg) {
  390. console.log(msg)
  391. }
  392. },
  393. mounted() {
  394. console.log(window['vue-cropper'])
  395. }
  396. })
  397. </script>
  398. </body>
  399. </html>