f461d31245669f2b6fdb7bd73d50476517789ddf.svn-base 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. <template>
  2. <div class="wrapper">
  3. <div class="model" v-show="model">
  4. <div class="model-show" @click="model = false">
  5. <img :src="modelSrc" alt="" @click="model = false">
  6. </div>
  7. </div>
  8. <div class="content">
  9. <div class="show-info">
  10. <div class="test test1">
  11. <vueCropper
  12. ref="cropper"
  13. :img="option.img"
  14. :outputSize="option.size"
  15. :outputType="option.outputType"
  16. :info="true"
  17. :full="option.full"
  18. :canMove="option.canMove"
  19. :canMoveBox="option.canMoveBox"
  20. :fixedBox="option.fixedBox"
  21. :original="option.original"
  22. :autoCrop="option.autoCrop"
  23. :autoCropWidth="option.autoCropWidth"
  24. :autoCropHeight="option.autoCropHeight"
  25. :centerBox="option.centerBox"
  26. :high="option.high"
  27. :infoTrue="option.infoTrue"
  28. :maxImgSize="option.maxImgSize"
  29. @realTime="realTime"
  30. @imgLoad="imgLoad"
  31. @cropMoving="cropMoving"
  32. :enlarge="option.enlarge"
  33. :mode="option.mode"
  34. :limitMinSize="option.limitMinSize"
  35. ></vueCropper>
  36. </div>
  37. <div class="test-button">
  38. <button @click="changeImg" class="btn">changeImg</button>
  39. <label class="btn" for="uploads">upload</label>
  40. <input type="file" id="uploads" style="position:absolute; clip:rect(0 0 0 0);" accept="image/png, image/jpeg, image/gif, image/jpg" @change="uploadImg($event, 1)" ref="uploadImg">
  41. <button @click="startCrop" v-if="!crap" class="btn">start</button>
  42. <button @click="stopCrop" v-else class="btn">stop</button>
  43. <button @click="clearCrop" class="btn">clear</button>
  44. <button @click="refreshCrop" class="btn">refresh</button>
  45. <button @click="changeScale(1)" class="btn">+</button>
  46. <button @click="changeScale(-1)" class="btn">-</button>
  47. <button @click="rotateLeft" class="btn">rotateLeft</button>
  48. <button @click="rotateRight" class="btn">rotateRight</button>
  49. <button @click="finish('base64')" class="btn">preview(base64)</button>
  50. <button @click="finish('blob')" class="btn">preview(blob)</button>
  51. <button @click="() => option.img = ''" class="btn">清除图片</button>
  52. <a @click="down('base64')" class="btn">download(base64)</a>
  53. <a @click="down('blob')" class="btn">download(blob)</a>
  54. <a :href="downImg" download="demo.png" ref="downloadDom"></a>
  55. </div>
  56. <div class="pre">
  57. <section class="pre-item">
  58. <p>截图框大小</p>
  59. <div class="show-preview" :style="{'width': previews.w + 'px', 'height': previews.h + 'px', 'overflow': 'hidden',
  60. 'margin': '5px'}">
  61. <div :style="previews.div">
  62. <img :src="previews.url" :style="previews.img">
  63. </div>
  64. </div>
  65. </section>
  66. <section class="pre-item">
  67. <p>中等大小</p>
  68. <div :style="previewStyle1">
  69. <div :style="previews.div">
  70. <img :src="previews.url" :style="previews.img">
  71. </div>
  72. </div>
  73. </section>
  74. <section class="pre-item">
  75. <p>迷你大小</p>
  76. <div :style="previewStyle2">
  77. <div :style="previews.div">
  78. <img :src="previews.url" :style="previews.img">
  79. </div>
  80. </div>
  81. </section>
  82. <section class="pre-item" title="zoom: (100 / previews.w)">
  83. <p>固定为100宽度</p>
  84. <div :style="previewStyle3">
  85. <div :style="previews.div">
  86. <img :src="previews.url" :style="previews.img">
  87. </div>
  88. </div>
  89. </section>
  90. <section class="pre-item" title="zoom: (100 / previews.h)">
  91. <p>固定为100高度</p>
  92. <div :style="previewStyle4">
  93. <div :style="previews.div">
  94. <img :src="previews.url" :style="previews.img">
  95. </div>
  96. </div>
  97. </section>
  98. </div>
  99. <div style="display:block; width: 100%;">
  100. <label class="c-item">
  101. <span>图片默认渲染方式</span>
  102. <select v-model="option.mode">
  103. <option value="contain">contain</option>
  104. <option value="cover">cover</option>
  105. <option value="400px auto">400px auto</option>
  106. <option value="auto 400px">auto 400px</option>
  107. <option value="50%">50%</option>
  108. <option value="auto 50%">auto 50%</option>
  109. </select>
  110. <section>
  111. 类似css background属性设置 设置不符合规范不生效, 参照文档说明
  112. </section>
  113. </label>
  114. <label class="c-item">
  115. <span>上传时图片最大大小(默认会压缩尺寸到这个大小)</span>
  116. <input type="nubmer" v-model="option.maxImgSize">
  117. </label>
  118. <label class="c-item">
  119. <span>上传图片是否显示原始宽高 (针对大图 可以铺满)</span>
  120. <input type="checkbox" v-model="option.original">
  121. <span>original: {{ option.original}}</span>
  122. </label>
  123. <label class="c-item">
  124. <span>是否根据dpr生成适合屏幕的高清图片</span>
  125. <input type="checkbox" v-model="option.high">
  126. <span>high: {{ option.high}}</span>
  127. </label>
  128. <label class="c-item">
  129. <span>是否输出原图比例的截图</span>
  130. <input type="checkbox" v-model="option.full">
  131. <span>full: {{ option.full}}</span>
  132. </label>
  133. <label class="c-item">
  134. <span>截图信息展示是否是真实的输出宽高</span>
  135. <input type="checkbox" v-model="option.infoTrue">
  136. <span>infoTrue: {{ option.infoTrue}}</span>
  137. </label>
  138. <label class="c-item">
  139. <span>能否拖动图片</span>
  140. <input type="checkbox" v-model="option.canMove">
  141. <span>canMove: {{ option.canMove}}</span>
  142. </label>
  143. <label class="c-item">
  144. <span>能否拖动截图框</span>
  145. <input type="checkbox" v-model="option.canMoveBox">
  146. <span>canMoveBox: {{ option.canMoveBox}}</span>
  147. </label>
  148. <label class="c-item">
  149. <span>截图框固定大小</span>
  150. <input type="checkbox" v-model="option.fixedBox">
  151. <span>fixedBox: {{ option.fixedBox}}</span>
  152. </label>
  153. <label class="c-item">
  154. <span>是否自动生成截图框</span>
  155. <input type="checkbox" v-model="option.autoCrop">
  156. <span>autoCrop: {{ option.autoCrop}}</span>
  157. </label>
  158. <label class="c-item">
  159. <span>自动生成截图框的宽高</span>
  160. <span>宽度: </span><input type="number" v-model="option.autoCropWidth">
  161. <span>高度: </span><input type="number" v-model="option.autoCropHeight">
  162. </label>
  163. <label class="c-item">
  164. <span>截图框是否限制在图片里(只有在自动生成截图框时才能生效)</span>
  165. <input type="checkbox" v-model="option.centerBox">
  166. <span>centerBox: {{ option.centerBox}}</span>
  167. </label>
  168. <label class="c-item">
  169. <span>是否按照截图框比例输出 默认为1 </span>
  170. <input type="number" v-model="option.enlarge">
  171. </label>
  172. <p>输出图片格式</p>
  173. <label class="c-item">
  174. <label>jpg <input type="radio" name="type" value="jpeg" v-model="option.outputType"></label>
  175. <label>png <input type="radio" name="type" value="png" v-model="option.outputType"></label>
  176. <label>webp <input type="radio" name="type" value="webp" v-model="option.outputType"></label>
  177. </label>
  178. </div>
  179. </div>
  180. </div>
  181. </div>
  182. </template>
  183. <script>
  184. import { VueCropper } from 'vue-cropper'
  185. export default {
  186. name: 'ImagCropper',
  187. components: {
  188. VueCropper
  189. },
  190. data () {
  191. return {
  192. model: false,
  193. modelSrc: "",
  194. crap: false,
  195. previews: {},
  196. lists: [
  197. {img: "https://avatars2.githubusercontent.com/u/15681693?s=460&v=4"},
  198. {img: "http://cdn.xyxiao.cn/Landscape_1.jpg"},
  199. {img: "http://cdn.xyxiao.cn/Landscape_2.jpg"},
  200. {img: "http://cdn.xyxiao.cn/Landscape_3.jpg"},
  201. {img: "http://cdn.xyxiao.cn/Landscape_4.jpg"},
  202. {img: "http://cdn.xyxiao.cn/Portrait_1.jpg"},
  203. {img: "http://cdn.xyxiao.cn/Portrait_2.jpg"}
  204. ],
  205. option: {
  206. img: "",
  207. size: 1,
  208. full: false,
  209. outputType: "png",
  210. canMove: true,
  211. fixedBox: false,
  212. original: false,
  213. canMoveBox: true,
  214. autoCrop: true,
  215. // 只有自动截图开启 宽度高度才生效
  216. autoCropWidth: 200,
  217. autoCropHeight: 150,
  218. centerBox: false,
  219. high: false,
  220. cropData: {},
  221. enlarge: 1,
  222. mode: 'contain',
  223. maxImgSize: 3000,
  224. limitMinSize: [100, 120]
  225. },
  226. example2: {
  227. img: "http://cdn.xyxiao.cn/Landscape_2.jpg",
  228. info: true,
  229. size: 1,
  230. outputType: "jpeg",
  231. canScale: true,
  232. autoCrop: true,
  233. // 只有自动截图开启 宽度高度才生效
  234. autoCropWidth: 300,
  235. autoCropHeight: 250,
  236. fixed: true,
  237. // 真实的输出宽高
  238. infoTrue: true,
  239. fixedNumber: [4, 3]
  240. },
  241. example3: {
  242. img: "http://cdn.xyxiao.cn/Landscape_1.jpg",
  243. autoCrop: true,
  244. autoCropWidth: 200,
  245. autoCropHeight: 200,
  246. fixedBox: true
  247. },
  248. downImg: "#",
  249. previewStyle1: {},
  250. previewStyle2: {},
  251. previewStyle3: {},
  252. previewStyle4: {},
  253. code0: '',
  254. code1: '',
  255. code2: '',
  256. code3: '',
  257. preview3: '',
  258. };
  259. },
  260. methods: {
  261. changeImg() {
  262. this.option.img = this.lists[~~(Math.random() * this.lists.length)].img;
  263. },
  264. startCrop() {
  265. // start
  266. this.crap = true;
  267. this.$refs.cropper.startCrop();
  268. },
  269. stopCrop() {
  270. // stop
  271. this.crap = false;
  272. this.$refs.cropper.stopCrop();
  273. },
  274. clearCrop() {
  275. // clear
  276. this.$refs.cropper.clearCrop();
  277. },
  278. refreshCrop() {
  279. // clear
  280. this.$refs.cropper.refresh();
  281. },
  282. changeScale(num) {
  283. num = num || 1;
  284. this.$refs.cropper.changeScale(num);
  285. },
  286. rotateLeft() {
  287. this.$refs.cropper.rotateLeft();
  288. },
  289. rotateRight() {
  290. this.$refs.cropper.rotateRight();
  291. },
  292. finish(type) {
  293. if (type === "blob") {
  294. this.$refs.cropper.getCropBlob(data => {
  295. var img = window.URL.createObjectURL(data);
  296. this.model = true;
  297. this.modelSrc = img;
  298. });
  299. } else {
  300. this.$refs.cropper.getCropData(data => {
  301. this.model = true;
  302. this.modelSrc = data;
  303. });
  304. }
  305. },
  306. // 实时预览函数
  307. realTime(data) {
  308. var previews = data;
  309. var h = 0.5;
  310. var w = 0.2;
  311. this.previewStyle1 = {
  312. width: previews.w + "px",
  313. height: previews.h + "px",
  314. overflow: "hidden",
  315. margin: "0",
  316. zoom: h
  317. };
  318. this.previewStyle2 = {
  319. width: previews.w + "px",
  320. height: previews.h + "px",
  321. overflow: "hidden",
  322. margin: "0",
  323. zoom: w
  324. };
  325. this.previewStyle3 = {
  326. width: previews.w + "px",
  327. height: previews.h + "px",
  328. overflow: "hidden",
  329. margin: "0",
  330. zoom: (100 / previews.w)
  331. };
  332. this.previewStyle4 = {
  333. width: previews.w + "px",
  334. height: previews.h + "px",
  335. overflow: "hidden",
  336. margin: "0",
  337. zoom: (100 / previews.h)
  338. };
  339. this.previews = data;
  340. },
  341. finish2(type) {
  342. this.$refs.cropper2.getCropData(data => {
  343. this.model = true;
  344. this.modelSrc = data;
  345. });
  346. },
  347. finish3(type) {
  348. this.$refs.cropper3.getCropData(data => {
  349. this.model = true;
  350. this.modelSrc = data;
  351. });
  352. },
  353. down(type) {
  354. // 输出
  355. if (type === "blob") {
  356. this.$refs.cropper.getCropBlob(data => {
  357. this.downImg = window.URL.createObjectURL(data);
  358. if (window.navigator.msSaveBlob) {
  359. var blobObject = new Blob([data]);
  360. window.navigator.msSaveBlob(blobObject, "demo.png");
  361. } else {
  362. this.$nextTick(() => {
  363. this.$refs.downloadDom.click();
  364. });
  365. }
  366. });
  367. } else {
  368. this.$refs.cropper.getCropData(data => {
  369. this.downImg = data;
  370. if (window.navigator.msSaveBlob) {
  371. var blobObject = new Blob([data]);
  372. window.navigator.msSaveBlob(blobObject, "demo.png");
  373. } else {
  374. this.$nextTick(() => {
  375. this.$refs.downloadDom.click();
  376. });
  377. }
  378. });
  379. }
  380. },
  381. uploadImg(e, num) {
  382. //上传图片
  383. // this.option.img
  384. var file = e.target.files[0];
  385. if (!/\.(gif|jpg|jpeg|png|bmp|GIF|JPG|PNG)$/.test(e.target.value)) {
  386. alert("图片类型必须是.gif,jpeg,jpg,png,bmp中的一种");
  387. return false;
  388. }
  389. var reader = new FileReader();
  390. reader.onload = e => {
  391. let data;
  392. if (typeof e.target.result === "object") {
  393. // 把Array Buffer转化为blob 如果是base64不需要
  394. data = window.URL.createObjectURL(new Blob([e.target.result]));
  395. } else {
  396. data = e.target.result;
  397. }
  398. if (num === 1) {
  399. this.option.img = data;
  400. } else if (num === 2) {
  401. this.example2.img = data;
  402. }
  403. this.$refs.uploadImg.value = ''
  404. };
  405. // 转化为blob
  406. reader.readAsArrayBuffer(file);
  407. },
  408. imgLoad(msg) {
  409. console.log(msg);
  410. },
  411. cropMoving(data) {
  412. this.option.cropData = data;
  413. }
  414. },
  415. components: {
  416. VueCropper
  417. },
  418. mounted() {
  419. this.changeImg();
  420. var list = [].slice.call(document.querySelectorAll("pre code"));
  421. list.forEach((val, index) => {
  422. hljs.highlightBlock(val);
  423. });
  424. }
  425. };
  426. </script>
  427. <style scoped>
  428. * {
  429. margin: 0;
  430. padding: 0;
  431. }
  432. .content {
  433. margin: auto;
  434. max-width: 1200px;
  435. margin-bottom: 100px;
  436. }
  437. .test-button {
  438. display: flex;
  439. flex-wrap: wrap;
  440. }
  441. .btn {
  442. display: inline-block;
  443. line-height: 1;
  444. white-space: nowrap;
  445. cursor: pointer;
  446. background: #fff;
  447. border: 1px solid #c0ccda;
  448. color: #1f2d3d;
  449. text-align: center;
  450. box-sizing: border-box;
  451. outline: none;
  452. margin: 20px 10px 0px 0px;
  453. padding: 9px 15px;
  454. font-size: 14px;
  455. border-radius: 4px;
  456. color: #fff;
  457. background-color: #50bfff;
  458. border-color: #50bfff;
  459. transition: all 0.2s ease;
  460. text-decoration: none;
  461. user-select: none;
  462. }
  463. .des {
  464. line-height: 30px;
  465. }
  466. code.language-html {
  467. padding: 10px 20px;
  468. margin: 10px 0px;
  469. display: block;
  470. background-color: #333;
  471. color: #fff;
  472. overflow-x: auto;
  473. font-family: Consolas, Monaco, Droid, Sans, Mono, Source, Code, Pro, Menlo,
  474. Lucida, Sans, Type, Writer, Ubuntu, Mono;
  475. border-radius: 5px;
  476. white-space: pre;
  477. }
  478. .show-info {
  479. margin-bottom: 50px;
  480. }
  481. .show-info h2 {
  482. line-height: 50px;
  483. }
  484. .title {
  485. display: block;
  486. text-decoration: none;
  487. text-align: center;
  488. line-height: 1.5;
  489. margin: 20px 0px;
  490. background-image: -webkit-linear-gradient(
  491. left,
  492. #3498db,
  493. #f47920 10%,
  494. #d71345 20%,
  495. #f7acbc 30%,
  496. #ffd400 40%,
  497. #3498db 50%,
  498. #f47920 60%,
  499. #d71345 70%,
  500. #f7acbc 80%,
  501. #ffd400 90%,
  502. #3498db
  503. );
  504. color: transparent;
  505. -webkit-background-clip: text;
  506. background-size: 200% 100%;
  507. animation: slide 5s infinite linear;
  508. font-size: 40px;
  509. }
  510. .test {
  511. height: 500px;
  512. }
  513. .model {
  514. position: fixed;
  515. z-index: 10;
  516. width: 100vw;
  517. height: 100vh;
  518. overflow: auto;
  519. top: 0;
  520. left: 0;
  521. background: rgba(0, 0, 0, 0.8);
  522. }
  523. .model-show {
  524. display: flex;
  525. justify-content: center;
  526. align-items: center;
  527. width: 100vw;
  528. height: 100vh;
  529. text-align: center;
  530. }
  531. .model img {
  532. display: block;
  533. margin: auto;
  534. max-width: 80%;
  535. width: auto;
  536. user-select: none;
  537. background-position: 0px 0px, 10px 10px;
  538. background-size: 20px 20px;
  539. background-image: linear-gradient(
  540. 45deg,
  541. #eee 25%,
  542. transparent 25%,
  543. transparent 75%,
  544. #eee 75%,
  545. #eee 100%
  546. ),
  547. linear-gradient(45deg, #eee 25%, white 25%, white 75%, #eee 75%, #eee 100%);
  548. }
  549. .c-item {
  550. display: block;
  551. padding: 10px 0;
  552. user-select: none;
  553. }
  554. .pre {
  555. display: flex;
  556. flex-wrap: wrap;
  557. }
  558. .pre-item {
  559. padding-right: 20px;
  560. }
  561. @keyframes slide {
  562. 0% {
  563. background-position: 0 0;
  564. }
  565. 100% {
  566. background-position: -100% 0;
  567. }
  568. }
  569. @media screen and (max-width: 1000px) {
  570. .content {
  571. max-width: 90%;
  572. margin: auto;
  573. }
  574. .test {
  575. height: 400px;
  576. }
  577. }
  578. </style>