yq-avatar.vue 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  1. <template name="yq-avatar">
  2. <view>
  3. <image :src="imgSrc.imgSrc" @click="fSelect" :style="[ iS ]" mode="aspectFill" class="my-avatar"></image>
  4. <canvas canvas-id="avatar-canvas" id="avatar-canvas" class="my-canvas" :style="{top: sT, height: csH}"
  5. disable-scroll="false"></canvas>
  6. <canvas canvas-id="oper-canvas" id="oper-canvas" class="oper-canvas" :style="{top: sT, height: csH}"
  7. disable-scroll="false" @touchstart="fStart" @touchmove="fMove" @touchend="fEnd"></canvas>
  8. <canvas canvas-id="prv-canvas" id="prv-canvas" class="prv-canvas" disable-scroll="false"
  9. @touchstart="fHideImg" :style="{ height: csH, top: pT }"></canvas>
  10. <view class="oper-wrapper" :style="{display: sD, top:tp}">
  11. <view class="oper">
  12. <view class="btn-wrapper" v-if="sO">
  13. <view @click="fSelect" hover-class="hover" :style="{width: bW}"><text>重选</text></view>
  14. <view @click="fClose" hover-class="hover" :style="{width: bW}"><text>关闭</text></view>
  15. <view @click="fRotate" hover-class="hover" :style="{width: bW, display: bD}"><text>旋转</text></view>
  16. <view @click="fPreview" hover-class="hover" :style="{width: bW}"><text>预览</text></view>
  17. <view @click="fUpload" hover-class="hover" :style="{width: bW}"><text>上传</text></view>
  18. </view>
  19. <view class="clr-wrapper" v-else>
  20. <slider class="my-slider" @change="fColorChange" block-size="25" value="0" min="-100" max="100" activeColor="red"
  21. backgroundColor="green" block-color="grey" show-value></slider>
  22. <view @click="fPrvUpload" hover-class="hover" :style="{width: bW}"><text>上传</text></view>
  23. </view>
  24. </view>
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. "use strict";
  30. const tH = 50;
  31. export default {
  32. name: "yq-avatar",
  33. data() {
  34. return {
  35. csH: '0px',
  36. sD: 'none',
  37. sT: '-10000px',
  38. pT: '-10000px',
  39. iS: {},
  40. sS: {},
  41. sO: true,
  42. bW: '19%',
  43. bD: 'flex',
  44. tp: 0,
  45. imgSrc: {
  46. imgSrc: ''
  47. }
  48. };
  49. },
  50. watch: {
  51. avatarSrc() {
  52. this.imgSrc.imgSrc = this.avatarSrc;
  53. }
  54. },
  55. props: {
  56. avatarSrc: '',
  57. avatarStyle: '',
  58. selWidth: '',
  59. selHeight: '',
  60. expWidth: '',
  61. expHeight: '',
  62. minScale: '',
  63. maxScale: '',
  64. canScale: '',
  65. canRotate: '',
  66. lockWidth: '',
  67. lockHeight: '',
  68. stretch: '',
  69. lock: '',
  70. fileType: '',
  71. noTab: '',
  72. inner: '',
  73. quality: '',
  74. index: '',
  75. bgImage: '',
  76. },
  77. created() {
  78. this.cc = uni.createCanvasContext('avatar-canvas', this);
  79. this.cco = uni.createCanvasContext('oper-canvas', this);
  80. this.ccp = uni.createCanvasContext('prv-canvas', this);
  81. this.qlty = parseFloat(this.quality) || 1;
  82. this.imgSrc.imgSrc = this.avatarSrc;
  83. this.letRotate = (this.canRotate === false || this.inner === true || this.inner === 'true' || this.canRotate === 'false') ? 0 : 1;
  84. this.letScale = (this.canScale === false || this.canScale === 'false') ? 0 : 1;
  85. this.isin = (this.inner === true || this.inner === 'true') ? 1 : 0;
  86. this.indx = this.index || undefined;
  87. this.mnScale = parseFloat(this.minScale) || 0.3;
  88. this.mxScale = parseFloat(this.maxScale) || 4;
  89. this.noBar = (this.noTab === true || this.noTab === 'true') ? 1 : 0;
  90. this.stc = this.stretch;
  91. this.lck = this.lock;
  92. this.fType = this.fileType === 'jpg' ? 'jpg' : 'png';
  93. if (this.isin||!this.letRotate) {
  94. this.bW = '24%';
  95. this.bD = 'none';
  96. } else {
  97. this.bW = '19%';
  98. this.bD = 'flex';
  99. }
  100. if (this.noBar) {
  101. this.fWindowResize();
  102. } else {
  103. uni.showTabBar({
  104. fail: ()=>{
  105. this.noBar = 1;
  106. },
  107. success: ()=>{
  108. this.noBar = 0;
  109. },
  110. complete: (res) => {
  111. this.fWindowResize();
  112. }
  113. });
  114. }
  115. },
  116. methods: {
  117. fWindowResize() {
  118. let sysInfo = uni.getSystemInfoSync();
  119. this.platform = sysInfo.platform;
  120. this.wW = sysInfo.windowWidth;
  121. // #ifdef H5
  122. this.drawTop = sysInfo.windowTop;
  123. // #endif
  124. // #ifndef H5
  125. this.drawTop = 0;
  126. // #endif
  127. // #ifdef MP-ALIPAY
  128. this.wH = sysInfo.screenHeight - sysInfo.statusBarHeight - sysInfo.titleBarHeight;
  129. this.csH = this.wH - tH + 'px';
  130. // #endif
  131. // #ifndef MP-ALIPAY
  132. this.wH = sysInfo.windowHeight;
  133. if(!this.noBar) this.wH += tH;
  134. this.csH = this.wH - tH + 'px';
  135. // #endif
  136. this.tp = this.csH;
  137. // #ifdef H5
  138. this.tp = sysInfo.windowTop + parseInt(this.csH)+ 'px';
  139. // #endif
  140. this.pxRatio = this.wW / 750;
  141. let style = this.avatarStyle;
  142. if (style && style !== true && (style = style.trim())) {
  143. style = style.split(';');
  144. let obj = {};
  145. for (let v of style) {
  146. if (!v) continue;
  147. v = v.trim().split(':');
  148. if (v[1].toString().indexOf('upx') >= 0) {
  149. let arr = v[1].trim().split(' ');
  150. for (let k in arr) {
  151. if (!arr[k]) continue;
  152. if (arr[k].toString().indexOf('upx') >= 0) {
  153. arr[k] = parseFloat(arr[k]) * this.pxRatio + 'px';
  154. }
  155. }
  156. v[1] = arr.join(' ');
  157. }
  158. obj[v[0].trim()] = v[1].trim();
  159. }
  160. this.iS = obj;
  161. }
  162. this.expWidth && (this.eW = this.expWidth.toString().indexOf('upx') >= 0 ? parseInt(this.expWidth) * this.pxRatio :
  163. parseInt(this.expWidth));
  164. this.expHeight && (this.eH = this.expHeight.toString().indexOf('upx') >= 0 ? parseInt(this.expHeight) * this.pxRatio :
  165. parseInt(this.expHeight));
  166. if (this.sD === 'flex') {
  167. this.fDrawInit(true);
  168. }
  169. this.fHideImg();
  170. },
  171. fSelect() {
  172. if (this.fSelecting) return;
  173. this.fSelecting = true;
  174. setTimeout(() => {
  175. this.fSelecting = false;
  176. }, 500);
  177. uni.chooseImage({
  178. count: 1,
  179. sizeType: ['original', 'compressed'],
  180. sourceType: ['album', 'camera'],
  181. success: (r) => {
  182. // #ifdef MP-ALIPAY
  183. uni.showLoading();
  184. // #endif
  185. // #ifndef MP-ALIPAY
  186. uni.showLoading({
  187. title: '加载中...',
  188. mask: true
  189. });
  190. // #endif
  191. let path = this.imgPath = r.tempFilePaths[0];
  192. uni.getImageInfo({
  193. src: path,
  194. success: r => {
  195. this.imgWidth = r.width;
  196. this.imgHeight = r.height;
  197. this.path = path;
  198. if (!this.hasSel) {
  199. let style = this.sS || {};
  200. if (this.selWidth && this.selHeight) {
  201. let sW = this.selWidth.toString().indexOf('upx') >= 0 ? parseInt(this.selWidth) * this.pxRatio : parseInt(
  202. this.selWidth),
  203. sH = this.selHeight.toString().indexOf('upx') >= 0 ? parseInt(this.selHeight) * this.pxRatio : parseInt(
  204. this.selHeight);
  205. style.width = sW + 'px';
  206. style.height = sH + 'px';
  207. style.top = ((this.wH - sH - tH)|0) / 2 + 'px';
  208. style.left = ((this.wW - sW)|0) / 2 + 'px';
  209. } else {
  210. uni.showModal({
  211. title: '裁剪框的宽或高没有设置',
  212. showCancel: false
  213. })
  214. return;
  215. }
  216. this.sS = style;
  217. }
  218. if (this.noBar) {
  219. this.fDrawInit(true);
  220. } else {
  221. uni.hideTabBar({
  222. complete: () => {
  223. this.fDrawInit(true);
  224. }
  225. });
  226. }
  227. },
  228. fail: () => {
  229. uni.showToast({
  230. title: "请选择正确图片",
  231. duration: 2000,
  232. })
  233. },
  234. complete() {
  235. uni.hideLoading();
  236. }
  237. });
  238. }
  239. })
  240. },
  241. fUpload() {
  242. if (this.fUploading) return;
  243. this.fUploading = true;
  244. setTimeout(() => {
  245. this.fUploading = false;
  246. }, 1000)
  247. let style = this.sS,
  248. x = parseInt(style.left),
  249. y = parseInt(style.top),
  250. width = parseInt(style.width),
  251. height = parseInt(style.height),
  252. expWidth = this.eW || (width* this.pixelRatio),
  253. expHeight = this.eH || (height* this.pixelRatio);
  254. // #ifdef MP-ALIPAY
  255. uni.showLoading();
  256. // #endif
  257. // #ifndef MP-ALIPAY
  258. uni.showLoading({
  259. title: '加载中...',
  260. mask: true
  261. });
  262. // #endif
  263. this.sD = 'none';
  264. this.sT = '-10000px';
  265. this.hasSel = false;
  266. this.fHideImg();
  267. // #ifdef MP-ALIPAY
  268. this.cc.toTempFilePath({
  269. x: x,
  270. y: y,
  271. width: width,
  272. height: height,
  273. destWidth: expWidth,
  274. destHeight: expHeight,
  275. fileType: this.fType,
  276. quality: this.qlty,
  277. success: (r) => {
  278. r = r.apFilePath;
  279. this.$emit("upload", {
  280. avatar: this.imgSrc,
  281. path: r,
  282. index: this.indx,
  283. data: this.rtn,
  284. base64: this.base64 || null
  285. });
  286. },
  287. fail: (res) => {
  288. uni.showToast({
  289. title: "error1",
  290. duration: 2000,
  291. })
  292. },
  293. complete: () => {
  294. uni.hideLoading();
  295. this.noBar || uni.showTabBar();
  296. this.$emit("end");
  297. }
  298. });
  299. // #endif
  300. // #ifndef MP-ALIPAY
  301. uni.canvasToTempFilePath({
  302. x: x,
  303. y: y,
  304. width: width,
  305. height: height,
  306. destWidth: expWidth,
  307. destHeight: expHeight,
  308. canvasId: 'avatar-canvas',
  309. fileType: this.fType,
  310. quality: this.qlty,
  311. success: (r) => {
  312. r = r.tempFilePath;
  313. // #ifdef H5
  314. this.btop(r).then((r) => {
  315. this.$emit("upload", {
  316. avatar: this.imgSrc,
  317. path: r,
  318. index: this.indx,
  319. data: this.rtn,
  320. base64: this.base64 || null
  321. });
  322. return;
  323. })
  324. // #endif
  325. // #ifndef H5
  326. this.$emit("upload", {
  327. avatar: this.imgSrc,
  328. path: r,
  329. index: this.indx,
  330. data: this.rtn,
  331. base64: this.base64 || null
  332. });
  333. // #endif
  334. },
  335. fail: (res) => {
  336. uni.showToast({
  337. title: "error1",
  338. duration: 2000,
  339. })
  340. },
  341. complete: () => {
  342. uni.hideLoading();
  343. this.noBar || uni.showTabBar();
  344. this.$emit("end");
  345. }
  346. }, this);
  347. // #endif
  348. },
  349. fPrvUpload() {
  350. if (this.fPrvUploading) return;
  351. this.fPrvUploading = true;
  352. setTimeout(() => {
  353. this.fPrvUploading = false;
  354. }, 1000)
  355. let style = this.sS,
  356. destWidth = parseInt(style.width),
  357. destHeight = parseInt(style.height),
  358. prvX = this.prvX,
  359. prvY = this.prvY,
  360. prvWidth = this.prvWidth,
  361. prvHeight = this.prvHeight,
  362. expWidth = this.eW || (parseInt(style.width) * this.pixelRatio),
  363. expHeight = this.eH || (parseInt(style.height) * this.pixelRatio);
  364. // #ifdef MP-ALIPAY
  365. uni.showLoading();
  366. // #endif
  367. // #ifndef MP-ALIPAY
  368. uni.showLoading({
  369. title: '加载中...',
  370. mask: true
  371. });
  372. // #endif
  373. this.sD = 'none';
  374. this.sT = '-10000px';
  375. this.hasSel = false;
  376. this.fHideImg();
  377. // #ifdef MP-ALIPAY
  378. this.ccp.toTempFilePath({
  379. x: prvX,
  380. y: prvY,
  381. width: prvWidth,
  382. height: prvHeight,
  383. destWidth: expWidth,
  384. destHeight: expHeight,
  385. fileType: this.fType,
  386. quality: this.qlty,
  387. success: (r) => {
  388. r = r.apFilePath;
  389. this.$emit("upload", {
  390. avatar: this.imgSrc,
  391. path: r,
  392. index: this.indx,
  393. data: this.rtn,
  394. base64: this.base64 || null
  395. });
  396. },
  397. fail: () => {
  398. uni.showToast({
  399. title: "error_prv",
  400. duration: 2000,
  401. })
  402. },
  403. complete: () => {
  404. uni.hideLoading();
  405. this.noBar || uni.showTabBar();
  406. this.$emit("end");
  407. }
  408. });
  409. // #endif
  410. // #ifndef MP-ALIPAY
  411. uni.canvasToTempFilePath({
  412. x: prvX,
  413. y: prvY,
  414. width: prvWidth,
  415. height: prvHeight,
  416. destWidth: expWidth,
  417. destHeight: expHeight,
  418. canvasId: 'prv-canvas',
  419. fileType: this.fType,
  420. quality: this.qlty,
  421. success: (r) => {
  422. r = r.tempFilePath;
  423. // #ifdef H5
  424. this.btop(r).then((r) => {
  425. this.$emit("upload", {
  426. avatar: this.imgSrc,
  427. path: r,
  428. index: this.indx,
  429. data: this.rtn,
  430. base64: this.base64 || null
  431. });
  432. })
  433. // #endif
  434. // #ifndef H5
  435. this.$emit("upload", {
  436. avatar: this.imgSrc,
  437. path: r,
  438. index: this.indx,
  439. data: this.rtn,
  440. base64: this.base64 || null
  441. });
  442. // #endif
  443. },
  444. fail: () => {
  445. uni.showToast({
  446. title: "error_prv",
  447. duration: 2000,
  448. })
  449. },
  450. complete: () => {
  451. uni.hideLoading();
  452. this.noBar || uni.showTabBar();
  453. this.$emit("end");
  454. }
  455. }, this);
  456. // #endif
  457. },
  458. fDrawInit(ini = false) {
  459. let allWidth = this.wW,
  460. allHeight = this.wH,
  461. imgWidth = this.imgWidth,
  462. imgHeight = this.imgHeight,
  463. imgRadio = imgWidth / imgHeight,
  464. useWidth = allWidth - 40,
  465. useHeight = allHeight - tH - 80,
  466. useRadio = useWidth / useHeight,
  467. sW = parseInt(this.sS.width),
  468. sH = parseInt(this.sS.height);
  469. this.fixWidth = 0;
  470. this.fixHeight = 0;
  471. this.lckWidth = 0;
  472. this.lckHeight = 0;
  473. switch (this.stc) {
  474. case 'x':
  475. this.fixWidth = 1;
  476. break;
  477. case 'y':
  478. this.fixHeight = 1;
  479. break;
  480. case 'long':
  481. if (imgRadio > 1) this.fixWidth = 1;
  482. else this.fixHeight = 1;
  483. break;
  484. case 'short':
  485. if (imgRadio > 1) this.fixHeight = 1;
  486. else this.fixWidth = 1;
  487. break;
  488. case 'longSel':
  489. if (sW > sH) this.fixWidth = 1;
  490. else this.fixHeight = 1;
  491. break;
  492. case 'shortSel':
  493. if (sW > sH) this.fixHeight = 1;
  494. else this.fixWidth = 1;
  495. break;
  496. }
  497. switch (this.lck) {
  498. case 'x':
  499. this.lckWidth = 1;
  500. break;
  501. case 'y':
  502. this.lckHeight = 1;
  503. break;
  504. case 'long':
  505. if (imgRadio > 1) this.lckWidth = 1;
  506. else this.lckHeight = 1;
  507. break;
  508. case 'short':
  509. if (imgRadio > 1) this.lckHeight = 1;
  510. else this.lckWidth = 1;
  511. break;
  512. case 'longSel':
  513. if (sW > sH) this.lckWidth = 1;
  514. else this.lckHeight = 1;
  515. break;
  516. case 'shortSel':
  517. if (sW > sH) this.lckHeight = 1;
  518. else this.lckWidth = 1;
  519. break;
  520. }
  521. if (this.fixWidth) {
  522. useWidth = sW;
  523. useHeight = useWidth / imgRadio;
  524. } else if (this.fixHeight) {
  525. useHeight = sH;
  526. useWidth = useHeight * imgRadio;
  527. } else if (imgRadio < useRadio) {
  528. if (imgHeight < useHeight) {
  529. useWidth = imgWidth;
  530. useHeight = imgHeight;
  531. } else {
  532. useWidth = useHeight * imgRadio;
  533. }
  534. } else {
  535. if (imgWidth < useWidth) {
  536. useWidth = imgWidth;
  537. useHeight = imgHeight;
  538. } else {
  539. useHeight = useWidth / imgRadio;
  540. }
  541. }
  542. if (this.isin) {
  543. if (useWidth < sW) {
  544. useWidth = sW;
  545. useHeight = useWidth / imgRadio;
  546. this.lckHeight = 0;
  547. }
  548. if (useHeight < sH) {
  549. useHeight = sH;
  550. useWidth = useHeight * imgRadio;
  551. this.lckWidth = 0;
  552. }
  553. }
  554. this.scaleSize = 1;
  555. this.rotateDeg = 0;
  556. this.posWidth = (allWidth - useWidth) / 2 | 0;
  557. this.posHeight = (allHeight - useHeight - tH) / 2 | 0;
  558. this.useWidth = useWidth | 0;
  559. this.useHeight = useHeight | 0;
  560. this.centerX = this.posWidth + useWidth / 2;
  561. this.centerY = this.posHeight + useHeight / 2;
  562. this.focusX = 0;
  563. this.focusY = 0;
  564. let style = this.sS,
  565. left = parseInt(style.left),
  566. top = parseInt(style.top),
  567. width = parseInt(style.width),
  568. height = parseInt(style.height),
  569. canvas = this.canvas,
  570. canvasOper = this.canvasOper,
  571. cc = this.cc,
  572. cco = this.cco;
  573. cco.beginPath();
  574. cco.setLineWidth(3);
  575. cco.setGlobalAlpha(1);
  576. cco.setStrokeStyle('white');
  577. cco.strokeRect(left, top, width, height);
  578. cco.setFillStyle('black');
  579. cco.setGlobalAlpha(0.5);
  580. cco.fillRect(0, 0, this.wW, top);
  581. cco.fillRect(0, top, left, height);
  582. cco.fillRect(0, top + height, this.wW, this.wH - height - top - tH);
  583. cco.fillRect(left + width, top, this.wW - width - left, height);
  584. cco.setGlobalAlpha(1);
  585. cco.setStrokeStyle('red');
  586. cco.moveTo(left+15, top);
  587. cco.lineTo(left, top);
  588. cco.lineTo(left, top+15);
  589. cco.moveTo(left+width-15, top);
  590. cco.lineTo(left+width, top);
  591. cco.lineTo(left+width, top+15);
  592. cco.moveTo(left+15, top+height);
  593. cco.lineTo(left, top+height);
  594. cco.lineTo(left, top+height-15);
  595. cco.moveTo(left+width-15, top+height);
  596. cco.lineTo(left+width, top+height);
  597. cco.lineTo(left+width, top+height-15);
  598. cco.stroke();
  599. cco.draw(false, () => {
  600. if (ini) {
  601. this.sD = 'flex';
  602. this.sT = this.drawTop + 'px';
  603. this.fDrawImage(true);
  604. }
  605. });
  606. this.$emit("init");
  607. },
  608. fDrawImage(ini = false) {
  609. let tm_now = Date.now();
  610. if (tm_now - this.drawTm < 20) return;
  611. this.drawTm = tm_now;
  612. let cc = this.cc,
  613. imgWidth = this.useWidth * this.scaleSize,
  614. imgHeight = this.useHeight * this.scaleSize;
  615. // #ifdef MP-ALIPAY
  616. cc.save();
  617. // #endif
  618. if (this.bgImage) {
  619. // #ifdef MP-ALIPAY
  620. cc.clearRect(0, 0, this.wW, this.wH - tH);
  621. // #endif
  622. // #ifndef MP-ALIPAY
  623. cc.drawImage(this.bgImage, 0, 0, this.wW, this.wH - tH);
  624. // #endif
  625. } else {
  626. cc.fillRect(0, 0, this.wW, this.wH - tH);
  627. }
  628. if (this.isin) {
  629. let cx = this.focusX * (this.scaleSize - 1),
  630. cy = this.focusY * (this.scaleSize - 1);
  631. cc.translate(this.centerX, this.centerY);
  632. cc.rotate(this.rotateDeg * Math.PI / 180);
  633. cc.drawImage(this.imgPath, this.posWidth-this.centerX-cx, this.posHeight-this.centerY-cy, imgWidth, imgHeight);
  634. } else {
  635. cc.translate(this.posWidth + imgWidth / 2, this.posHeight + imgHeight / 2);
  636. cc.rotate(this.rotateDeg * Math.PI / 180);
  637. cc.drawImage(this.imgPath, -imgWidth / 2, -imgHeight / 2, imgWidth, imgHeight);
  638. }
  639. cc.draw(false);
  640. // #ifdef MP-ALIPAY
  641. cc.restore();
  642. // #endif
  643. },
  644. fPreview() {
  645. if (this.fPreviewing) return;
  646. this.fPreviewing = true;
  647. setTimeout(() => {
  648. this.fPreviewing = false;
  649. }, 1000);
  650. let style = this.sS,
  651. x = parseInt(style.left),
  652. y = parseInt(style.top),
  653. width = parseInt(style.width),
  654. height = parseInt(style.height);
  655. // #ifdef MP-ALIPAY
  656. uni.showLoading();
  657. // #endif
  658. // #ifndef MP-ALIPAY
  659. uni.showLoading({
  660. title: '加载中...',
  661. mask: true
  662. });
  663. // #endif
  664. // #ifdef MP-ALIPAY
  665. this.cc.toTempFilePath({
  666. x: x,
  667. y: y,
  668. width: width,
  669. height: height,
  670. expWidth: width * this.pixelRatio,
  671. expHeight: height * this.pixelRatio,
  672. fileType: this.fType,
  673. quality: this.qlty,
  674. success: (r) => {
  675. this.prvImgTmp = r = r.apFilePath;
  676. let ccp = this.ccp,
  677. prvX = this.wW,
  678. prvY = parseInt(this.csH),
  679. prvWidth = parseInt(this.sS.width),
  680. prvHeight = parseInt(this.sS.height),
  681. useWidth = prvX - 40,
  682. useHeight = prvY - 80,
  683. radio = useWidth / prvWidth,
  684. rHeight = prvHeight * radio;
  685. if (rHeight < useHeight) {
  686. prvWidth = useWidth;
  687. prvHeight = rHeight;
  688. } else {
  689. radio = useHeight / prvHeight;
  690. prvWidth *= radio;
  691. prvHeight = useHeight;
  692. }
  693. ccp.fillRect(0, 0, prvX, prvY);
  694. this.prvX = prvX = ((prvX - prvWidth) / 2) | 0;
  695. this.prvY = prvY = ((prvY - prvHeight) / 2) | 0;
  696. this.prvWidth = prvWidth = prvWidth | 0;
  697. this.prvHeight = prvHeight = prvHeight | 0;
  698. ccp.drawImage(r, prvX, prvY, prvWidth, prvHeight);
  699. ccp.draw(false);
  700. this.sO = false;
  701. this.pT = '0';
  702. },
  703. fail: () => {
  704. uni.showToast({
  705. title: "error2",
  706. duration: 2000,
  707. })
  708. },
  709. complete: () => {
  710. uni.hideLoading();
  711. }
  712. });
  713. // #endif
  714. // #ifndef MP-ALIPAY
  715. uni.canvasToTempFilePath({
  716. x: x,
  717. y: y,
  718. width: width,
  719. height: height,
  720. expWidth: width * this.pixelRatio,
  721. expHeight: height * this.pixelRatio,
  722. canvasId: 'avatar-canvas',
  723. fileType: this.fType,
  724. quality: this.qlty,
  725. success: (r) => {
  726. this.prvImgTmp = r = r.tempFilePath;
  727. let ccp = this.ccp,
  728. prvX = this.wW,
  729. prvY = parseInt(this.csH);
  730. // #ifndef H5||MP-WEIXIN||APP-PLUS
  731. prvY += tH;
  732. // #endif
  733. // #ifdef APP-PLUS
  734. if (this.platform === 'android') {
  735. prvY += tH;
  736. }
  737. // #endif
  738. let prvWidth = parseInt(this.sS.width),
  739. prvHeight = parseInt(this.sS.height),
  740. useWidth = prvX - 40,
  741. useHeight = prvY - 80,
  742. radio = useWidth / prvWidth,
  743. rHeight = prvHeight * radio;
  744. if (rHeight < useHeight) {
  745. prvWidth = useWidth;
  746. prvHeight = rHeight;
  747. } else {
  748. radio = useHeight / prvHeight;
  749. prvWidth *= radio;
  750. prvHeight = useHeight;
  751. }
  752. ccp.fillRect(0, 0, prvX, prvY);
  753. this.prvX = prvX = ((prvX - prvWidth) / 2) | 0;
  754. this.prvY = prvY = ((prvY - prvHeight) / 2) | 0;
  755. this.prvWidth = prvWidth = prvWidth | 0;
  756. this.prvHeight = prvHeight = prvHeight | 0;
  757. ccp.drawImage(r, prvX, prvY, prvWidth, prvHeight);
  758. ccp.draw(false);
  759. // #ifdef H5
  760. this.btop(r).then((r) => {
  761. this.sO = false;
  762. this.pT = this.drawTop + 'px';
  763. })
  764. // #endif
  765. this.sO = false;
  766. // if (this.platform === 'android') this.sO = false;
  767. this.pT = this.drawTop + 'px';
  768. },
  769. fail: () => {
  770. uni.showToast({
  771. title: "error2",
  772. duration: 2000,
  773. })
  774. },
  775. complete: () => {
  776. uni.hideLoading();
  777. }
  778. }, this);
  779. // #endif
  780. },
  781. fChooseImg(index = undefined, params = undefined, data = undefined) {
  782. if (params) {
  783. let sW = params.selWidth,
  784. sH = params.selHeight,
  785. expWidth = params.expWidth,
  786. expHeight = params.expHeight,
  787. quality = params.quality,
  788. canRotate = params.canRotate,
  789. canScale = params.canScale,
  790. minScale = params.minScale,
  791. maxScale = params.maxScale,
  792. stretch = params.stretch,
  793. fileType = params.fileType,
  794. inner = params.inner,
  795. lock = params.lock;
  796. expWidth && (this.eW = expWidth.toString().indexOf('upx') >= 0 ? parseInt(expWidth) * this.pxRatio : parseInt(
  797. expWidth));
  798. expHeight && (this.eH = expHeight.toString().indexOf('upx') >= 0 ? parseInt(expHeight) * this.pxRatio : parseInt(
  799. expHeight));
  800. this.letRotate = (canRotate === false || inner === true || inner === 'true' || canRotate === 'false') ? 0 : 1;
  801. this.letScale = (canScale === false || canScale === 'false') ? 0 : 1;
  802. this.qlty = parseFloat(quality) || 1;
  803. this.mnScale = parseFloat(minScale) || 0.3;
  804. this.mxScale = parseFloat(maxScale) || 4;
  805. this.stc = stretch;
  806. this.isin = (inner === true || inner === 'true') ? 1 : 0;
  807. this.fType = fileType === 'jpg' ? 'jpg' : 'png';
  808. this.lck = lock;
  809. if (this.isin||!this.letRotate) {
  810. this.bW = '24%';
  811. this.bD = 'none';
  812. } else {
  813. this.bW = '19%';
  814. this.bD = 'flex';
  815. }
  816. if (sW && sH) {
  817. sW = sW.toString().indexOf('upx') >= 0 ? parseInt(sW) * this.pxRatio : parseInt(sW);
  818. sH = sH.toString().indexOf('upx') >= 0 ? parseInt(sH) * this.pxRatio : parseInt(sH);
  819. this.sS.width = sW + 'px';
  820. this.sS.height = sH + 'px';
  821. this.sS.top = ((this.wH - sH - tH)|0) / 2 + 'px';
  822. this.sS.left = ((this.wW - sW)|0) / 2 + 'px';
  823. this.hasSel = true;
  824. }
  825. }
  826. this.rtn = data;
  827. this.indx = index;
  828. this.fSelect();
  829. },
  830. fRotate() {
  831. this.rotateDeg += 90 - this.rotateDeg % 90;
  832. this.fDrawImage();
  833. },
  834. fStart(e) {
  835. let touches = e.touches,
  836. touch0 = touches[0],
  837. touch1 = touches[1];
  838. this.touch0 = touch0;
  839. this.touch1 = touch1;
  840. if (touch1) {
  841. let x = touch1.x - touch0.x,
  842. y = touch1.y - touch0.y;
  843. this.fgDistance = Math.sqrt(x * x + y * y);
  844. }
  845. },
  846. fMove(e) {
  847. let touches = e.touches,
  848. touch0 = touches[0],
  849. touch1 = touches[1];
  850. if (touch1) {
  851. let x = touch1.x - touch0.x,
  852. y = touch1.y - touch0.y,
  853. fgDistance = Math.sqrt(x * x + y * y),
  854. scaleSize = 0.005 * (fgDistance - this.fgDistance),
  855. beScaleSize = this.scaleSize + scaleSize;
  856. do {
  857. if (!this.letScale) break;
  858. if (beScaleSize < this.mnScale) break;
  859. if (beScaleSize > this.mxScale) break;
  860. let growX = this.useWidth * scaleSize / 2,
  861. growY = this.useHeight * scaleSize / 2;
  862. if (this.isin) {
  863. let imgWidth = this.useWidth * beScaleSize,
  864. imgHeight = this.useHeight * beScaleSize,
  865. l = this.posWidth - growX,
  866. t = this.posHeight - growY,
  867. r = l + imgWidth,
  868. b = t + imgHeight,
  869. left = parseInt(this.sS.left),
  870. top = parseInt(this.sS.top),
  871. width = parseInt(this.sS.width),
  872. height = parseInt(this.sS.height),
  873. right = left + width,
  874. bottom = top + height,
  875. cx, cy;
  876. if (imgWidth <= width || imgHeight <= height) break;
  877. this.cx = cx = this.focusX * beScaleSize - this.focusX,
  878. this.cy = cy = this.focusY * beScaleSize - this.focusY;
  879. this.posWidth -= growX;
  880. this.posHeight -= growY;
  881. if (this.posWidth - cx > left) {
  882. this.posWidth = left + cx;
  883. }
  884. if (this.posWidth + imgWidth - cx < right) {
  885. this.posWidth = right - imgWidth + cx;
  886. }
  887. if (this.posHeight - cy > top) {
  888. this.posHeight = top + cy;
  889. }
  890. if (this.posHeight + imgHeight - cy < bottom) {
  891. this.posHeight = bottom - imgHeight + cy;
  892. }
  893. } else {
  894. this.posWidth -= growX;
  895. this.posHeight -= growY;
  896. }
  897. this.scaleSize = beScaleSize;
  898. } while (0);
  899. this.fgDistance = fgDistance;
  900. if (touch1.x !== touch0.x && this.letRotate) {
  901. x = (this.touch1.y - this.touch0.y) / (this.touch1.x - this.touch0.x);
  902. y = (touch1.y - touch0.y) / (touch1.x - touch0.x);
  903. this.rotateDeg += Math.atan((y - x) / (1 + x * y)) * 180 / Math.PI;
  904. this.touch0 = touch0;
  905. this.touch1 = touch1;
  906. }
  907. this.fDrawImage();
  908. } else if (this.touch0) {
  909. let x = touch0.x - this.touch0.x,
  910. y = touch0.y - this.touch0.y,
  911. beX = this.posWidth + x,
  912. beY = this.posHeight + y;
  913. if (this.isin) {
  914. let imgWidth = this.useWidth * this.scaleSize,
  915. imgHeight = this.useHeight * this.scaleSize,
  916. l = beX,
  917. t = beY,
  918. r = l + imgWidth,
  919. b = t + imgHeight,
  920. left = parseInt(this.sS.left),
  921. top = parseInt(this.sS.top),
  922. right = left + parseInt(this.sS.width),
  923. bottom = top + parseInt(this.sS.height),
  924. cx, cy;
  925. this.cx = cx = this.focusX * this.scaleSize - this.focusX;
  926. this.cy = cy = this.focusY * this.scaleSize - this.focusY;
  927. if (!this.lckWidth && Math.abs(x) < 100) {
  928. if (left < l - cx) {
  929. this.posWidth = left + cx;
  930. } else if (right > r - cx) {
  931. this.posWidth = right - imgWidth + cx;
  932. } else {
  933. this.posWidth = beX;
  934. this.focusX -= x;
  935. }
  936. }
  937. if (!this.lckHeight && Math.abs(y) < 100) {
  938. if (top < t - cy) {
  939. this.focusY -= (top + cy - this.posHeight);
  940. this.posHeight = top + cy;
  941. } else if (bottom > b - cy) {
  942. this.focusY -= (bottom + cy - (this.posHeight + imgHeight));
  943. this.posHeight = bottom - imgHeight + cy;
  944. } else {
  945. this.posHeight = beY;
  946. this.focusY -= y;
  947. }
  948. }
  949. } else {
  950. if (Math.abs(x) < 100 && !this.lckWidth) this.posWidth = beX;
  951. if (Math.abs(y) < 100 && !this.lckHeight) this.posHeight = beY;
  952. this.focusX -= x;
  953. this.focusY -= y;
  954. }
  955. this.touch0 = touch0;
  956. this.fDrawImage();
  957. }
  958. },
  959. fEnd(e) {
  960. let touches = e.touches,
  961. touch0 = touches && touches[0],
  962. touch1 = touches && touches[1];
  963. if (touch0) {
  964. this.touch0 = touch0;
  965. } else {
  966. this.touch0 = null;
  967. this.touch1 = null;
  968. }
  969. },
  970. fHideImg() {
  971. this.prvImg = '';
  972. this.pT = '-10000px';
  973. this.sO = true;
  974. this.prvImgData = null;
  975. this.target = null;
  976. },
  977. fClose() {
  978. this.sD = 'none';
  979. this.sT = '-10000px';
  980. this.hasSel = false;
  981. this.fHideImg();
  982. this.noBar || uni.showTabBar();
  983. this.$emit("end");
  984. },
  985. fGetImgData() {
  986. return new Promise((resolve, reject) => {
  987. let prvX = this.prvX,
  988. prvY = this.prvY,
  989. prvWidth = this.prvWidth,
  990. prvHeight = this.prvHeight;
  991. // #ifdef MP-ALIPAY
  992. this.ccp.getImageData({
  993. x: prvX,
  994. y: prvY,
  995. width: prvWidth,
  996. height: prvHeight,
  997. success(res) {
  998. resolve(res.data);
  999. },
  1000. fail(err) {
  1001. reject(err);
  1002. }
  1003. }, this);
  1004. // #endif
  1005. // #ifndef MP-ALIPAY
  1006. uni.canvasGetImageData({
  1007. canvasId: 'prv-canvas',
  1008. x: prvX,
  1009. y: prvY,
  1010. width: prvWidth,
  1011. height: prvHeight,
  1012. success(res) {
  1013. resolve(res.data);
  1014. },
  1015. fail(err) {
  1016. reject(err);
  1017. }
  1018. }, this);
  1019. // #endif
  1020. });
  1021. },
  1022. async fColorChange(e) {
  1023. let tm_now = Date.now();
  1024. if (tm_now - this.prvTm < 100) return;
  1025. this.prvTm = tm_now;
  1026. // #ifdef MP-ALIPAY
  1027. uni.showLoading();
  1028. // #endif
  1029. // #ifndef MP-ALIPAY
  1030. uni.showLoading({
  1031. title: '加载中...',
  1032. mask: true
  1033. });
  1034. // #endif
  1035. if (!this.prvImgData) {
  1036. if (!(this.prvImgData = await this.fGetImgData().catch(() => {
  1037. uni.showToast({
  1038. title: "error_read",
  1039. duration: 2000,
  1040. })
  1041. }))) return;
  1042. this.target = new Uint8ClampedArray(this.prvImgData.length);
  1043. }
  1044. let data = this.prvImgData,
  1045. target = this.target,
  1046. i = e.detail.value,
  1047. r, g, b, a, h, s, l, d, p, q, t, min, max, hK, tR, tG, tB;
  1048. if (i === 0) {
  1049. target = data;
  1050. } else {
  1051. i = (i + 100) / 200;
  1052. if (i < 0.005) i = 0;
  1053. if (i > 0.995) i = 1;
  1054. for (let n = data.length - 1; n >= 0; n -= 4) {
  1055. r = data[n - 3] / 255;
  1056. g = data[n - 2] / 255;
  1057. b = data[n - 1] / 255;
  1058. max = Math.max(r, g, b);
  1059. min = Math.min(r, g, b);
  1060. d = max - min;
  1061. if (max === min) {
  1062. h = 0;
  1063. } else if (max === r && g >= b) {
  1064. h = 60 * ((g - b) / d);
  1065. } else if (max === r && g < b) {
  1066. h = 60 * ((g - b) / d) + 360;
  1067. } else if (max === g) {
  1068. h = 60 * ((b - r) / d) + 120;
  1069. } else if (max === b) {
  1070. h = 60 * ((r - g) / d) + 240;
  1071. }
  1072. l = (max + min) / 2;
  1073. if (l === 0 || max === min) {
  1074. s = 0;
  1075. } else if (0 < l && l <= 0.5) {
  1076. s = d / (2 * l);
  1077. } else if (l > 0.5) {
  1078. s = d / (2 - 2 * l);
  1079. }
  1080. data[n] && (a = data[n]);
  1081. if (i < 0.5) {
  1082. s = s * i / 0.5;
  1083. } else if (i > 0.5) {
  1084. s = 2 * s + 2 * i - (s * i / 0.5) - 1;
  1085. }
  1086. if (s === 0) {
  1087. r = g = b = Math.round(l * 255);
  1088. } else {
  1089. if (l < 0.5) {
  1090. q = l * (1 + s);
  1091. } else if (l >= 0.5) {
  1092. q = l + s - (l * s);
  1093. }
  1094. p = 2 * l - q;
  1095. hK = h / 360;
  1096. tR = hK + 1 / 3;
  1097. tG = hK;
  1098. tB = hK - 1 / 3;
  1099. let correctRGB = (t) => {
  1100. if (t < 0) {
  1101. return t + 1.0;
  1102. }
  1103. if (t > 1) {
  1104. return t - 1.0;
  1105. }
  1106. return t;
  1107. };
  1108. let createRGB = (t) => {
  1109. if (t < (1 / 6)) {
  1110. return p + ((q - p) * 6 * t);
  1111. } else if (t >= (1 / 6) && t < (1 / 2)) {
  1112. return q;
  1113. } else if (t >= (1 / 2) && t < (2 / 3)) {
  1114. return p + ((q - p) * 6 * ((2 / 3) - t));
  1115. }
  1116. return p;
  1117. };
  1118. r = tR = Math.round(createRGB(correctRGB(tR)) * 255);
  1119. g = tG = Math.round(createRGB(correctRGB(tG)) * 255);
  1120. b = tB = Math.round(createRGB(correctRGB(tB)) * 255);
  1121. }
  1122. a && (target[n] = a);
  1123. target[n - 3] = r;
  1124. target[n - 2] = g;
  1125. target[n - 1] = b;
  1126. }
  1127. }
  1128. let prvX = this.prvX,
  1129. prvY = this.prvY,
  1130. prvWidth = this.prvWidth,
  1131. prvHeight = this.prvHeight;
  1132. // #ifdef MP-ALIPAY
  1133. this.ccp.putImageData({
  1134. x: prvX,
  1135. y: prvY,
  1136. width: prvWidth,
  1137. height: prvHeight,
  1138. data: target,
  1139. fail() {
  1140. uni.showToast({
  1141. title: 'error_put',
  1142. duration: 2000
  1143. })
  1144. },
  1145. complete() {
  1146. uni.hideLoading();
  1147. }
  1148. }, this);
  1149. // #endif
  1150. // #ifndef MP-ALIPAY
  1151. uni.canvasPutImageData({
  1152. canvasId: 'prv-canvas',
  1153. x: prvX,
  1154. y: prvY,
  1155. width: prvWidth,
  1156. height: prvHeight,
  1157. data: target,
  1158. fail() {
  1159. uni.showToast({
  1160. title: 'error_put',
  1161. duration: 2000
  1162. })
  1163. },
  1164. complete() {
  1165. uni.hideLoading();
  1166. }
  1167. }, this);
  1168. // #endif
  1169. },
  1170. btop(base64) {
  1171. this.base64 = base64;
  1172. return new Promise(function(resolve, reject) {
  1173. var arr = base64.split(','),
  1174. mime = arr[0].match(/:(.*?);/)[1],
  1175. bstr = atob(arr[1]),
  1176. n = bstr.length,
  1177. u8arr = new Uint8Array(n);
  1178. while (n--) {
  1179. u8arr[n] = bstr.charCodeAt(n);
  1180. }
  1181. return resolve((window.URL || window.webkitURL).createObjectURL(new Blob([u8arr], {
  1182. type: mime
  1183. })));
  1184. });
  1185. },
  1186. }
  1187. }
  1188. </script>
  1189. <style>
  1190. .my-canvas {
  1191. display: flex;
  1192. position: fixed !important;
  1193. background: #000000;
  1194. left: 0;
  1195. z-index: 100000;
  1196. width: 100%;
  1197. }
  1198. .my-avatar {
  1199. width: 150upx;
  1200. height: 150upx;
  1201. border-radius: 100%;
  1202. }
  1203. .oper-canvas {
  1204. display: flex;
  1205. position: fixed !important;
  1206. left: 0;
  1207. z-index: 100001;
  1208. width: 100%;
  1209. }
  1210. .prv-canvas {
  1211. display: flex;
  1212. position: fixed !important;
  1213. background: #000000;
  1214. left: 0;
  1215. z-index: 200000;
  1216. width: 100%;
  1217. }
  1218. .oper-wrapper {
  1219. height: 50px;
  1220. position: fixed !important;
  1221. box-sizing: border-box;
  1222. border: 1px solid #F1F1F1;
  1223. background: #ffffff;
  1224. width: 100%;
  1225. left: 0;
  1226. bottom: 0;
  1227. z-index: 100009;
  1228. flex-direction: row;
  1229. }
  1230. .oper {
  1231. display: flex;
  1232. flex-direction: column;
  1233. justify-content: center;
  1234. padding: 10upx 20upx;
  1235. width: 100%;
  1236. height: 100%;
  1237. box-sizing: border-box;
  1238. align-self: center;
  1239. }
  1240. .btn-wrapper {
  1241. display: flex;
  1242. flex-direction: row;
  1243. /* #ifndef H5 */
  1244. flex-grow: 1;
  1245. /* #endif */
  1246. /* #ifdef H5 */
  1247. height: 50px;
  1248. /* #endif */
  1249. justify-content: space-between;
  1250. }
  1251. .btn-wrapper view {
  1252. display: flex;
  1253. align-items: center;
  1254. justify-content: center;
  1255. font-size: 16px;
  1256. color: #333;
  1257. border: 1px solid #f1f1f1;
  1258. border-radius: 6%;
  1259. }
  1260. .hover {
  1261. background: #f1f1f1;
  1262. border-radius: 6%;
  1263. }
  1264. .clr-wrapper {
  1265. display: flex;
  1266. flex-direction: row;
  1267. flex-grow: 1;
  1268. }
  1269. .clr-wrapper view {
  1270. display: flex;
  1271. align-items: center;
  1272. justify-content: center;
  1273. font-size: 16px;
  1274. color: #333;
  1275. border: 1px solid #f1f1f1;
  1276. border-radius: 6%;
  1277. }
  1278. .my-slider {
  1279. flex-grow: 1;
  1280. }
  1281. </style>