vue3.html 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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@vue3</title>
  8. <link rel="stylesheet" href="../next/dist/index.css">
  9. <style>
  10. * {
  11. margin: 0;
  12. padding: 0;
  13. }
  14. .cut {
  15. width: 500px;
  16. height: 500px;
  17. margin: 30px auto;
  18. }
  19. .c-item {
  20. max-width: 800px;
  21. margin: 10px auto;
  22. margin-top: 20px;
  23. }
  24. .content {
  25. margin: auto;
  26. max-width: 1200px;
  27. margin-bottom: 100px;
  28. }
  29. .test-button {
  30. display: flex;
  31. flex-wrap: wrap;
  32. align-content: center;
  33. justify-content: center;
  34. }
  35. .btn {
  36. display: inline-block;
  37. line-height: 1;
  38. white-space: nowrap;
  39. cursor: pointer;
  40. background: #fff;
  41. border: 1px solid #c0ccda;
  42. color: #1f2d3d;
  43. text-align: center;
  44. box-sizing: border-box;
  45. outline: none;
  46. margin: 20px 10px 0px 0px;
  47. padding: 9px 15px;
  48. font-size: 14px;
  49. border-radius: 4px;
  50. color: #fff;
  51. background-color: #50bfff;
  52. border-color: #50bfff;
  53. transition: all .2s ease;
  54. text-decoration: none;
  55. user-select: none;
  56. }
  57. .des {
  58. line-height: 30px;
  59. }
  60. code.language-html {
  61. padding: 10px 20px;
  62. margin: 10px 0px;
  63. display: block;
  64. background-color: #333;
  65. color: #fff;
  66. overflow-x: auto;
  67. font-family: Consolas, Monaco, Droid, Sans, Mono, Source, Code, Pro, Menlo, Lucida, Sans, Type, Writer, Ubuntu, Mono;
  68. border-radius: 5px;
  69. white-space: pre;
  70. }
  71. .show-info {
  72. margin-bottom: 50px;
  73. }
  74. .show-info h2 {
  75. line-height: 50px;
  76. }
  77. /*.title, .title:hover, .title-focus, .title:visited {
  78. color: black;
  79. }*/
  80. .title {
  81. display: block;
  82. text-decoration: none;
  83. text-align: center;
  84. line-height: 1.5;
  85. margin: 20px 0px;
  86. 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);
  87. color: transparent;
  88. -webkit-background-clip: text;
  89. background-size: 200% 100%;
  90. animation: slide 5s infinite linear;
  91. font-size: 40px;
  92. }
  93. .test {
  94. height: 500px;
  95. }
  96. .model {
  97. position: fixed;
  98. z-index: 10;
  99. width: 100vw;
  100. height: 100vh;
  101. overflow: auto;
  102. top: 0;
  103. left: 0;
  104. background: rgba(0, 0, 0, 0.8);
  105. }
  106. .model-show {
  107. display: flex;
  108. justify-content: center;
  109. align-items: center;
  110. width: 100vw;
  111. height: 100vh;
  112. }
  113. .model img {
  114. display: block;
  115. margin: auto;
  116. max-width: 80%;
  117. user-select: none;
  118. background-position: 0px 0px, 10px 10px;
  119. background-size: 20px 20px;
  120. 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%);
  121. }
  122. .c-item {
  123. display: block;
  124. user-select: none;
  125. }
  126. @keyframes slide {
  127. 0% {
  128. background-position: 0 0;
  129. }
  130. 100% {
  131. background-position: -100% 0;
  132. }
  133. }
  134. </style>
  135. </head>
  136. <body>
  137. <div id="app">
  138. <div class="model" v-show="model" @click="model = false">
  139. <div class="model-show">
  140. <img :src="modelSrc" alt="">
  141. </div>
  142. </div>
  143. <p>例子</p>
  144. <div class="cut">
  145. <vue-cropper ref="cropper" v-bind="option"></vue-cropper>
  146. </div>
  147. <div class="test-button">
  148. <button @click="changeImg" class="btn">changeImg</button>
  149. <label class="btn" for="uploads">upload</label>
  150. <input type="file" id="uploads" style="position:absolute; clip:rect(0 0 0 0);"
  151. accept="image/png, image/jpeg, image/gif, image/jpg" @change="uploadImg($event, 1)">
  152. <button @click="startCrop" v-if="!crap" class="btn">start</button>
  153. <button @click="stopCrop" v-else class="btn">stop</button>
  154. <button @click="clearCrop" class="btn">clear</button>
  155. <button @click="refreshCrop" class="btn">refresh</button>
  156. <button @click="changeScale(1)" class="btn">+</button>
  157. <button @click="changeScale(-1)" class="btn">-</button>
  158. <button @click="rotateLeft" class="btn">rotateLeft</button>
  159. <button @click="rotateRight" class="btn">rotateRight</button>
  160. <button @click="finish('base64')" class="btn">preview(base64)</button>
  161. <button @click="finish('blob')" class="btn">preview(blob)</button>
  162. <a @click="down('base64')" class="btn">download(base64)</a>
  163. <a @click="down('blob')" class="btn">download(blob)</a>
  164. <div style="display:block; width: 100%;">
  165. <label class="c-item">
  166. <span>图片默认渲染方式</span>
  167. <select v-model="option.mode">
  168. <option value="contain">contain</option>
  169. <option value="cover">cover</option>
  170. <option value="400px auto">400px auto</option>
  171. <option value="auto 400px">auto 400px</option>
  172. <option value="50%">50%</option>
  173. <option value="auto 50%">auto 50%</option>
  174. </select>
  175. <section>
  176. 类似css background属性设置 设置不符合规范不生效, 参照文档说明
  177. </section>
  178. </label>
  179. <label class="c-item">
  180. <span>上传时图片最大大小(默认会压缩尺寸到这个大小)</span>
  181. <input type="nubmer" v-model="option.maxImgSize">
  182. </label>
  183. <label class="c-item">
  184. <span>上传图片是否显示原始宽高 (针对大图 可以铺满)</span>
  185. <input type="checkbox" v-model="option.original">
  186. <span>original: {{ option.original}}</span>
  187. </label>
  188. <label class="c-item">
  189. <span>是否根据dpr生成适合屏幕的高清图片</span>
  190. <input type="checkbox" v-model="option.high">
  191. <span>high: {{ option.high}}</span>
  192. </label>
  193. <label class="c-item">
  194. <span>是否输出原图比例的截图</span>
  195. <input type="checkbox" v-model="option.full">
  196. <span>full: {{ option.full}}</span>
  197. </label>
  198. <label class="c-item">
  199. <span>截图信息展示是否是真实的输出宽高</span>
  200. <input type="checkbox" v-model="option.infoTrue">
  201. <span>infoTrue: {{ option.infoTrue}}</span>
  202. </label>
  203. <label class="c-item">
  204. <span>能否拖动图片</span>
  205. <input type="checkbox" v-model="option.canMove">
  206. <span>canMove: {{ option.canMove}}</span>
  207. </label>
  208. <label class="c-item">
  209. <span>能否拖动截图框</span>
  210. <input type="checkbox" v-model="option.canMoveBox">
  211. <span>canMoveBox: {{ option.canMoveBox}}</span>
  212. </label>
  213. <label class="c-item">
  214. <span>截图框固定大小</span>
  215. <input type="checkbox" v-model="option.fixedBox">
  216. <span>fixedBox: {{ option.fixedBox}}</span>
  217. </label>
  218. <label class="c-item">
  219. <span>是否自动生成截图框</span>
  220. <input type="checkbox" v-model="option.autoCrop">
  221. <span>autoCrop: {{ option.autoCrop}}</span>
  222. </label>
  223. <label class="c-item">
  224. <span>是否开启截图框比例</span>
  225. <input type="checkbox" v-model="option.fixed">
  226. <span>fixed: {{ option.fixed}}</span>
  227. </label>
  228. <label class="c-item">
  229. <span>固定截图框比例</span>
  230. <span>宽度: </span><input type="number" v-model="option.fixedNumber[0]">
  231. <span>高度: </span><input type="number" v-model="option.fixedNumber[1]">
  232. </label>
  233. <label class="c-item">
  234. <span>截图框的最小限制</span>
  235. <span>宽度: </span><input type="number" v-model="option.limitMinSize[0]">
  236. <span>高度: </span><input type="number" v-model="option.limitMinSize[1]">
  237. </label>
  238. <label class="c-item">
  239. <span>自动生成截图框的宽高</span>
  240. <span>宽度: </span><input type="number" v-model="option.autoCropWidth">
  241. <span>高度: </span><input type="number" v-model="option.autoCropHeight">
  242. </label>
  243. <label class="c-item">
  244. <span>截图框是否限制在图片里(只有在自动生成截图框时才能生效)</span>
  245. <input type="checkbox" v-model="option.centerBox">
  246. <span>centerBox: {{ option.centerBox}}</span>
  247. </label>
  248. <label class="c-item">
  249. <span>是否按照截图框比例输出 默认为1 </span>
  250. <input type="number" v-model="option.enlarge">
  251. </label>
  252. <p>输出图片格式</p>
  253. <label class="c-item">
  254. <label>jpg <input type="radio" name="type" value="jpeg" v-model="option.outputType"></label>
  255. <label>png <input type="radio" name="type" value="png" v-model="option.outputType"></label>
  256. <label>webp <input type="radio" name="type" value="webp" v-model="option.outputType"></label>
  257. </label>
  258. </div>
  259. </div>
  260. <div class="show-preview"
  261. :style="{'width': previews.w + 'px', 'height': previews.h + 'px', 'overflow': 'hidden', 'margin': '5px'}">
  262. <div :style="previews.div">
  263. <img :src="previews.url" :style="previews.img">
  264. </div>
  265. </div>
  266. </div>
  267. <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  268. <script src="../next/dist/vue-cropper.umd.js"></script>
  269. <script>
  270. const app = Vue.createApp({
  271. data: function () {
  272. return {
  273. model: false,
  274. modelSrc: '',
  275. crap: false,
  276. previews: {},
  277. lists: [
  278. // {
  279. // img: 'https://fengyuanchen.github.io/cropper/images/picture.jpg'
  280. // },
  281. {
  282. img: "https://avatars2.githubusercontent.com/u/15681693?s=460&v=4"
  283. },
  284. {
  285. img: "http://cdn.xyxiao.cn/Landscape_1.jpg"
  286. },
  287. {
  288. img: "http://cdn.xyxiao.cn/Landscape_2.jpg"
  289. },
  290. {
  291. img: "http://cdn.xyxiao.cn/Landscape_3.jpg"
  292. },
  293. {
  294. img: "http://cdn.xyxiao.cn/Landscape_4.jpg"
  295. },
  296. {
  297. img: "http://cdn.xyxiao.cn/Portrait_1.jpg"
  298. },
  299. {
  300. img: "http://cdn.xyxiao.cn/Portrait_2.jpg"
  301. }
  302. ],
  303. option: {
  304. img: "https://avatars2.githubusercontent.com/u/15681693?s=460&v=4",
  305. size: 1,
  306. full: false,
  307. outputType: "png",
  308. canMove: true,
  309. fixedBox: false,
  310. original: false,
  311. canMoveBox: true,
  312. autoCrop: true,
  313. // 只有自动截图开启 宽度高度才生效
  314. autoCropWidth: 200,
  315. autoCropHeight: 150,
  316. centerBox: false,
  317. high: false,
  318. cropData: {},
  319. enlarge: 1,
  320. mode: 'contain',
  321. maxImgSize: 3000,
  322. limitMinSize: [50, 50],
  323. fixed: false,
  324. fixedNumber: [2, 1],
  325. },
  326. show: true
  327. }
  328. },
  329. methods: {
  330. changeImg() {
  331. this.option.img = this.lists[~~(Math.random() * this.lists.length)].img
  332. },
  333. startCrop() {
  334. // start
  335. this.crap = true
  336. this.$refs.cropper.startCrop()
  337. },
  338. stopCrop() {
  339. // stop
  340. this.crap = false
  341. this.$refs.cropper.stopCrop()
  342. },
  343. clearCrop() {
  344. // clear
  345. this.$refs.cropper.clearCrop()
  346. },
  347. refreshCrop() {
  348. // clear
  349. this.$refs.cropper.refresh()
  350. },
  351. changeScale(num) {
  352. num = num || 1
  353. this.$refs.cropper.changeScale(num)
  354. },
  355. rotateLeft() {
  356. this.$refs.cropper.rotateLeft()
  357. },
  358. rotateRight() {
  359. this.$refs.cropper.rotateRight()
  360. },
  361. finish(type) {
  362. // 输出
  363. // var test = window.open('about:blank')
  364. // test.document.body.innerHTML = '图片生成中..'
  365. if (type === 'blob') {
  366. this.$refs.cropper.getCropBlob((data) => {
  367. var img = window.URL.createObjectURL(data)
  368. this.model = true
  369. this.modelSrc = img
  370. })
  371. } else {
  372. this.$refs.cropper.getCropData((data) => {
  373. this.model = true
  374. this.modelSrc = data
  375. })
  376. }
  377. },
  378. // 实时预览函数
  379. realTime(data) {
  380. console.log(data)
  381. this.previews = data
  382. },
  383. finish2(type) {
  384. this.$refs.cropper2.getCropData((data) => {
  385. this.model = true
  386. this.modelSrc = data
  387. })
  388. },
  389. finish3(type) {
  390. this.$refs.cropper3.getCropData((data) => {
  391. this.model = true
  392. this.modelSrc = data
  393. })
  394. },
  395. down(type) {
  396. // event.preventDefault()
  397. var aLink = document.createElement('a')
  398. aLink.download = 'demo'
  399. // 输出
  400. if (type === 'blob') {
  401. this.$refs.cropper.getCropBlob((data) => {
  402. this.downImg = window.URL.createObjectURL(data)
  403. aLink.href = window.URL.createObjectURL(data)
  404. aLink.click()
  405. })
  406. } else {
  407. this.$refs.cropper.getCropData((data) => {
  408. this.downImg = data
  409. aLink.href = data
  410. aLink.click()
  411. })
  412. }
  413. },
  414. uploadImg(e, num) {
  415. //上传图片
  416. // this.option.img
  417. var file = e.target.files[0]
  418. if (!/\.(gif|jpg|jpeg|png|bmp|GIF|JPG|PNG)$/.test(e.target.value)) {
  419. alert('图片类型必须是.gif,jpeg,jpg,png,bmp中的一种')
  420. return false
  421. }
  422. var reader = new FileReader()
  423. reader.onload = (e) => {
  424. let data
  425. if (typeof e.target.result === 'object') {
  426. // 把Array Buffer转化为blob 如果是base64不需要
  427. data = window.URL.createObjectURL(new Blob([e.target.result]))
  428. } else {
  429. data = e.target.result
  430. }
  431. if (num === 1) {
  432. this.option.img = data
  433. } else if (num === 2) {
  434. this.example2.img = data
  435. }
  436. }
  437. // 转化为base64
  438. // reader.readAsDataURL(file)
  439. // 转化为blob
  440. reader.readAsArrayBuffer(file)
  441. },
  442. imgLoad(msg) {
  443. console.log(msg)
  444. }
  445. },
  446. mounted() {
  447. console.log(window['vue-cropper'])
  448. }
  449. })
  450. app.component('vue-cropper', window['vue-cropper'].VueCropper)
  451. app.mount('#app')
  452. </script>
  453. </body>
  454. </html>