index.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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-cropper3</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"
  145. :info="true" :full="option.full" :can-move="option.canMove" :can-move-box="option.canMoveBox"
  146. :fixed-box="option.fixedBox" :original="option.original" :auto-crop="option.autoCrop"
  147. :auto-crop-width="option.autoCropWidth" :auto-crop-height="option.autoCropHeight" :center-box="option.centerBox"
  148. @real-time="realTime" :high="option.high"></vue-cropper>
  149. </div>
  150. <div class="test-button">
  151. <button @click="changeImg" class="btn">changeImg</button>
  152. <label class="btn" for="uploads">upload</label>
  153. <input type="file" id="uploads" style="position:absolute; clip:rect(0 0 0 0);"
  154. accept="image/png, image/jpeg, image/gif, image/jpg" @change="uploadImg($event, 1)">
  155. <button @click="startCrop" v-if="!crap" class="btn">start</button>
  156. <button @click="stopCrop" v-else class="btn">stop</button>
  157. <button @click="clearCrop" class="btn">clear</button>
  158. <button @click="refreshCrop" class="btn">refresh</button>
  159. <button @click="changeScale(1)" class="btn">+</button>
  160. <button @click="changeScale(-1)" class="btn">-</button>
  161. <button @click="rotateLeft" class="btn">rotateLeft</button>
  162. <button @click="rotateRight" class="btn">rotateRight</button>
  163. <button @click="finish('base64')" class="btn">preview(base64)</button>
  164. <button @click="finish('blob')" class="btn">preview(blob)</button>
  165. <a @click="down('base64')" class="btn">download(base64)</a>
  166. <a @click="down('blob')" class="btn">download(blob)</a>
  167. <div style="display:block; width: 100%;">
  168. <label class="c-item">
  169. <span>上传图片是否显示原始宽高 (针对大图 可以铺满)</span>
  170. <input type="checkbox" v-model="option.original">
  171. <span>original: {{ option.original}}</span>
  172. </label>
  173. <label class="c-item">
  174. <span>能否拖动图片</span>
  175. <input type="checkbox" v-model="option.canMove">
  176. </label>
  177. <label class="c-item">
  178. <span>能否拖动截图框</span>
  179. <input type="checkbox" v-model="option.canMoveBox">
  180. <span>canMoveBox: {{ option.canMoveBox}}</span>
  181. </label>
  182. <label class="c-item">
  183. <span>截图框固定大小</span>
  184. <input type="checkbox" v-model="option.fixedBox">
  185. <span>fixedBox: {{ option.fixedBox}}</span>
  186. </label>
  187. <label class="c-item">
  188. <span>是否输出原图比例的截图</span>
  189. <input type="checkbox" v-model="option.full">
  190. <span>full: {{ option.full}}</span>
  191. </label>
  192. <label class="c-item">
  193. <span>是否自动生成截图框</span>
  194. <input type="checkbox" v-model="option.autoCrop">
  195. <span>autoCrop: {{ option.autoCrop}}</span>
  196. </label>
  197. <label class="c-item">
  198. <span>是否根据dpr生成适合屏幕的高清图片</span>
  199. <input type="checkbox" v-model="option.high">
  200. <span>high: {{ option.high}}</span>
  201. </label>
  202. <label class="c-item">
  203. <span>截图框是否限制在图片里(只有在自动生成截图框时才能生效)</span>
  204. <input type="checkbox" v-model="option.centerBox">
  205. <span>centerBox: {{ option.centerBox}}</span>
  206. </label>
  207. <label class="c-item">
  208. <p>输出图片格式</p>
  209. <label>jpg
  210. <input type="radio" name="type" value="jpeg" v-model="option.outputType">
  211. </label>
  212. <label>png
  213. <input type="radio" name="type" value="png" v-model="option.outputType">
  214. </label>
  215. <label>webp
  216. <input type="radio" name="type" value="webp" v-model="option.outputType">
  217. </label>
  218. </label>
  219. </div>
  220. </div>
  221. <div class="show-preview"
  222. :style="{'width': previews.w + 'px', 'height': previews.h + 'px', 'overflow': 'hidden', 'margin': '5px'}">
  223. <div :style="previews.div">
  224. <img :src="previews.url" :style="previews.img">
  225. </div>
  226. </div>
  227. </div>
  228. <!-- <script src="https://vuejs.org/js/vue.min.js"></script> -->
  229. <script src="https://cdn.jsdelivr.net/npm/vue@3.2.37/dist/vue.global.js"></script>
  230. <script src="../dist/vue-cropper.umd.js"></script>
  231. <script>
  232. // Vue.use(window['vue-cropper'])
  233. const app = Vue.createApp({
  234. data () {
  235. return {
  236. model: false,
  237. modelSrc: '',
  238. crap: false,
  239. previews: {},
  240. lists: [{
  241. img: 'http://ofyaji162.bkt.clouddn.com/touxiang.jpg'
  242. },
  243. {
  244. img: 'https://o90cnn3g2.qnssl.com/0C3ABE8D05322EAC3120DDB11F9D1F72.png'
  245. },
  246. {
  247. img: 'http://ofyaji162.bkt.clouddn.com/bg1.jpg',
  248. },
  249. {
  250. img: 'http://ofyaji162.bkt.clouddn.com/bg2.jpg',
  251. },
  252. {
  253. img: 'http://ofyaji162.bkt.clouddn.com/can.jpg'
  254. },
  255. {
  256. img: 'http://ofyaji162.bkt.clouddn.com/nightcat.jpg'
  257. }
  258. ],
  259. option: {
  260. img: 'https://avatars3.githubusercontent.com/u/15681693',
  261. size: 1,
  262. full: false,
  263. outputType: 'png',
  264. canMove: true,
  265. fixedBox: false,
  266. original: false,
  267. canMoveBox: true,
  268. autoCrop: true,
  269. // 只有自动截图开启 宽度高度才生效
  270. autoCropWidth: 200,
  271. autoCropHeight: 150,
  272. centerBox: false,
  273. high: true
  274. },
  275. show: true
  276. }
  277. },
  278. methods: {
  279. changeImg() {
  280. this.option.img = this.lists[~~(Math.random() * this.lists.length)].img
  281. },
  282. startCrop() {
  283. // start
  284. this.crap = true
  285. this.$refs.cropper.startCrop()
  286. },
  287. stopCrop() {
  288. // stop
  289. this.crap = false
  290. this.$refs.cropper.stopCrop()
  291. },
  292. clearCrop() {
  293. // clear
  294. this.$refs.cropper.clearCrop()
  295. },
  296. refreshCrop() {
  297. // clear
  298. this.$refs.cropper.refresh()
  299. },
  300. changeScale(num) {
  301. num = num || 1
  302. this.$refs.cropper.changeScale(num)
  303. },
  304. rotateLeft() {
  305. this.$refs.cropper.rotateLeft()
  306. },
  307. rotateRight() {
  308. this.$refs.cropper.rotateRight()
  309. },
  310. finish(type) {
  311. // 输出
  312. // var test = window.open('about:blank')
  313. // test.document.body.innerHTML = '图片生成中..'
  314. if (type === 'blob') {
  315. this.$refs.cropper.getCropBlob((data) => {
  316. var img = window.URL.createObjectURL(data)
  317. this.model = true
  318. this.modelSrc = img
  319. })
  320. } else {
  321. this.$refs.cropper.getCropData((data) => {
  322. this.model = true
  323. this.modelSrc = data
  324. })
  325. }
  326. },
  327. // 实时预览函数
  328. realTime(data) {
  329. console.log(data)
  330. this.previews = data
  331. },
  332. finish2(type) {
  333. this.$refs.cropper2.getCropData((data) => {
  334. this.model = true
  335. this.modelSrc = data
  336. })
  337. },
  338. finish3(type) {
  339. this.$refs.cropper3.getCropData((data) => {
  340. this.model = true
  341. this.modelSrc = data
  342. })
  343. },
  344. down(type) {
  345. // event.preventDefault()
  346. var aLink = document.createElement('a')
  347. aLink.download = 'demo'
  348. // 输出
  349. if (type === 'blob') {
  350. this.$refs.cropper.getCropBlob((data) => {
  351. this.downImg = window.URL.createObjectURL(data)
  352. aLink.href = window.URL.createObjectURL(data)
  353. aLink.click()
  354. })
  355. } else {
  356. this.$refs.cropper.getCropData((data) => {
  357. this.downImg = data
  358. aLink.href = data
  359. aLink.click()
  360. })
  361. }
  362. },
  363. uploadImg(e, num) {
  364. //上传图片
  365. // this.option.img
  366. var file = e.target.files[0]
  367. if (!/\.(gif|jpg|jpeg|png|bmp|GIF|JPG|PNG)$/.test(e.target.value)) {
  368. alert('图片类型必须是.gif,jpeg,jpg,png,bmp中的一种')
  369. return false
  370. }
  371. var reader = new FileReader()
  372. reader.onload = (e) => {
  373. let data
  374. if (typeof e.target.result === 'object') {
  375. // 把Array Buffer转化为blob 如果是base64不需要
  376. data = window.URL.createObjectURL(new Blob([e.target.result]))
  377. } else {
  378. data = e.target.result
  379. }
  380. if (num === 1) {
  381. this.option.img = data
  382. } else if (num === 2) {
  383. this.example2.img = data
  384. }
  385. }
  386. // 转化为base64
  387. // reader.readAsDataURL(file)
  388. // 转化为blob
  389. reader.readAsArrayBuffer(file)
  390. },
  391. imgLoad(msg) {
  392. console.log(msg)
  393. }
  394. },
  395. mounted() {
  396. console.log(window['vue-cropper'])
  397. }
  398. })
  399. app.component('vue-cropper', window['vue-cropper'].VueCropper)
  400. app.mount('#app')
  401. </script>
  402. </body>
  403. </html>