欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

微信小程序畫布顯示圖片繪制矩形選區(qū)效果

 更新時間:2024年05月25日 10:38:24   作者:xiejunna  
這篇文章主要介紹了微信小程序畫布顯示圖片繪制矩形選區(qū)效果,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧

wxml

<view class="page-body">
  <!-- 畫布 -->
  <view class="page-body-wrapper">
    <canvas canvas-id="myCanvas" type="2d" id="myCanvas" class='myCanvas' bindtouchstart="touchStart" bindtouchmove="touchMove" bindtouchend="touchEnd"></canvas>
  </view>
  <!-- 操作 -->
  <view class="layout-bottom">
    <view class="page-bottom">
      <view class="pbottom pmiddle" bindtap="pre">
        <image src="/images/next2.png" style="height: 65rpx; width: 65rpx; " mode="aspectFit"></image>
        <view class="pictureBottomArea"><p>返回</p></view>
      </view>
      <view class="pbottom pmiddle" bindtap="detection">
        <image src="{{sbUrl}}" style="height: 100rpx; width: 100rpx; " mode="aspectFit"></image>
        <view class="pictureBottomArea1"><p>識別</p></view>
      </view>
      <view class="pbottom pmiddle" bindtap="clear">
        <image src="/images/qc3.png" style="height: 70rpx; width: 70rpx; " mode="aspectFit"></image>
        <view class="pictureBottomArea"><p>清除選區(qū)</p></view>
      </view>
    </view>
  </view>
</view>

wxss

.myCanvas { background-color: #F7F7F7; width: 100vw; height: 100vh; }
page { width: 100%; height: 100%; padding: 0; margin: 0; background-color: #F8F8F8; font-size: 32rpx; line-height: 1.6; display: flex; display: -webkit-flex; flex-direction: column; flex-wrap: wrap; justify-content: center; align-items: center; }
.page-body { width: 100%; height: 100%; padding: 0; margin: 0; }  .page-body-wrapper { width: 100%; height: 80%; display: flex; flex-direction: column; align-items: center; width: 100%; }
.layout-bottom { width: 100%; height: 20%; background-color: white; }
.page-bottom { width: 100%; height: 75%; display: flex; display: -webkit-flex; flex-direction: row; flex-wrap: wrap; justify-content: center; align-items: center; }
.pbottom { width: 33.333333333%; height: 100%; }
.pmiddle{ display: flex; display: -webkit-flex; flex-direction: column; flex-wrap: wrap; justify-content: center; align-items: center; }
.pictureBottomArea { margin-top: 15rpx; font-size: small; }
.pictureBottomArea1 { font-size: 0.9rem; letter-spacing:4rpx; font-weight: 600; color: #585858; }

js

圖片適配畫布顯示,關鍵在于計數(shù)圖片縮放比例

// 定義變量
let startX, startY, endX, endY, rectWidth, rectHeight
Page({
  data: {
    drawWidth: 0,
    drawHeight: 0,
    drawX: 0,
    drawY: 0,
    ratio: 0,//縮放比例
    sbUrl: '/images/a2.png',//按鈕
    imgSrc: '/images/ll.png',
    area: [],
    ctx: null,
    canvas: null,
    drawimage: null,
  },
  onLoad(options) {
    startX = 0
    startY = 0
    endX = 0
    endY = 0
    rectWidth = 0
    rectHeight = 0
    //把圖片繪制到畫布上
    this.drawImage(this.data.imgSrc)
  },
  //把圖片繪制到畫布上
  drawImage(imgSrc){
    let _this = this
    wx.createSelectorQuery().select('#myCanvas').fields({ node: true, size: true }).exec((res0) => {
      //獲取canvas寬高
      const canvas = res0[0].node
      console.log(canvas)
      let ctx = canvas.getContext('2d');
      const cw = res0[0].width
      const ch = res0[0].height
      console.log('Canvas寬度:'+cw, 'Canvas高度:'+ch)
      const dpr = wx.getSystemInfoSync().pixelRatio
      console.log(dpr)
      canvas.width = cw * dpr     // 獲取寬
      canvas.height = ch * dpr  // 獲取高
      console.log(cw * dpr, ch * dpr)
      ctx.scale(dpr, dpr)
      wx.getImageInfo({
        src: imgSrc,
        success: function (res) {
          //獲取圖片寬高
          let iw = res.width
          let ih = res.height
          console.log('圖片寬度:'+iw, '圖片高度:'+ih);
          // 計算繪制位置,保持原始比例
          let ratio = Math.min(cw / iw, ch / ih);
          console.log(ratio)
          // 圖片適配畫布顯示,關鍵在于計數(shù)圖片縮放比例
          let drawWidth = iw * ratio;
          let drawHeight = ih * ratio;
          console.log('圖片縮放后寬度:'+drawWidth, '圖片縮放后高度:'+drawHeight);
          let drawX = (cw - drawWidth) / 2;
          let drawY = (ch - drawHeight) / 2;
          // 到這里就可以直接繪制
          let image = canvas.createImage();//創(chuàng)建iamge實例
          image.src = imgSrc;  // 引入本地圖片
          image.onload = function () {
            ctx.drawImage(image, 0, 0, drawWidth, drawHeight);
          }
          _this.setData({drawWidth: drawWidth,drawHeight: drawHeight,drawX: drawX,drawY: drawY,  ratio: ratio,ctx: ctx,canvas: canvas,drawimage: image})
        },
        fail: function (res) {
          console.error('獲取圖片信息失敗', res);
        }
      });
    })
  },
  // 觸摸開始事件
  touchStart(e) {
    startX = e.touches[0].x
    startY = e.touches[0].y
    console.log("觸摸開始事件", e.touches[0], startX, startY)
  },
  // 觸摸移動事件
  touchMove(e) {
    let imgSrc = this.data.imgSrc
    let drawWidth = this.data.drawWidth
    let drawHeight = this.data.drawHeight
    let ctx = this.data.ctx
    let image = this.data.drawimage
    endX = e.touches[0].x
    endY = e.touches[0].y
    ctx.clearRect(0, 0, drawWidth, drawHeight)
    ctx.drawImage(image, 0, 0, drawWidth, drawHeight);
    rectWidth = endX - startX
    rectHeight = endY - startY
    ctx.strokeRect(startX, startY, rectWidth, rectHeight)
    ctx.strokeStyle = 'red'
    ctx.stroke()
  },
  // 觸摸結束事件
  touchEnd(e) {
    // 繪制完成后的操作
    // 可以將坐標框的位置和大小保存到全局變量或發(fā)送給服務器等
    console.log("觸摸結束事件",e.changedTouches[0])
  },
  //清除繪制的圖形
  clear(){
    console.log("清除繪制")
    let imgSrc = this.data.imgSrc
    let drawWidth = this.data.drawWidth
    let drawHeight = this.data.drawHeight
    let ctx = this.data.ctx
    let image = this.data.drawimage
    ctx.clearRect(0, 0, drawWidth, drawHeight)
    ctx.drawImage(image, 0, 0, drawWidth, drawHeight);
    startX = 0
    startY = 0
    endX = 0
    endY = 0
    rectWidth = 0
    rectHeight = 0
  },
  // 識別
  detection(e){
    console.log("開始識別")
    let ratio = this.data.ratio
    //獲取繪制選區(qū)的相關信息,這里要除以圖片縮放比例,才是真是圖片上的框選區(qū)
    if (rectWidth != 0 && rectHeight != 0){
        console.log('矩形','x='+startX/ratio,'y='+startY/ratio,'Width='+rectWidth/ratio,'Height='+rectHeight/ratio)
     }
  },
  //上一頁
  pre(){
    console.log("上一頁")
  }
})

到此這篇關于微信小程序畫布顯示圖片繪制矩形選區(qū)的文章就介紹到這了,更多相關微信小程序圖片繪制矩形選區(qū)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 詳解微信小程序——自定義圓形進度條

    詳解微信小程序——自定義圓形進度條

    這篇文章主要介紹了詳解微信小程序——自定義圓形進度條,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2016-12-12
  • js原生Ajax的封裝和原理詳解

    js原生Ajax的封裝和原理詳解

    這篇文章主要為大家詳細介紹了js原生Ajax的封裝和原理,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-03-03
  • JS與SQL方式隨機生成高強度密碼示例

    JS與SQL方式隨機生成高強度密碼示例

    這篇文章主要介紹了JS與SQL方式隨機生成高強度密碼,結合實例形式分析了javascript方式與SQL方式生成高強度密碼的相關操作技巧,需要的朋友可以參考下
    2018-12-12
  • iScroll.js 使用方法參考

    iScroll.js 使用方法參考

    以下這篇文章是iScroll.js官網(wǎng)的中文翻譯,盡管自己英文不好,但覺得原作者們翻譯的這個資料還是可以的,基本用法介紹清楚了。如果你英文比較好的話,可以看看官網(wǎng)的資料
    2016-05-05
  • JavaScript操作select元素和option的實例代碼

    JavaScript操作select元素和option的實例代碼

    這篇文章主要介紹了JavaScript操作select元素和option的實例代碼的相關資料,需要的朋友可以參考下
    2016-01-01
  • JS事件監(jiān)聽與事件委托舉例詳解(前端小白必學)

    JS事件監(jiān)聽與事件委托舉例詳解(前端小白必學)

    在JavaScript的學習中我們經(jīng)常會遇到JavaScript的事件機制,例如,事件綁定、事件監(jiān)聽、事件委托(事件代理)等,下面這篇文章主要給大家介紹了關于JS事件監(jiān)聽與事件委托的相關資料,需要的朋友可以參考下
    2024-07-07
  • 關于JSONP跨域請求原理的深入解析

    關于JSONP跨域請求原理的深入解析

    JSONP(JSON?with?Padding)是JSON的一種“使用模式”,可用于解決主流瀏覽器的跨域數(shù)據(jù)訪問的問題,這篇文章主要給大家介紹了關于JSONP跨域請求原理的相關資料,需要的朋友可以參考下
    2022-01-01
  • jquery form表單獲取內(nèi)容以及綁定數(shù)據(jù)

    jquery form表單獲取內(nèi)容以及綁定數(shù)據(jù)

    這篇文章主要介紹了jquery form表單獲取內(nèi)容以及form表單綁定數(shù)據(jù),獲取表單的數(shù)據(jù)保存到數(shù)據(jù)庫,或者將數(shù)據(jù)綁定到form表單,感興趣的小伙伴們可以參考一下
    2016-02-02
  • javascript判斷元素存在和判斷元素存在于實時的dom中的方法

    javascript判斷元素存在和判斷元素存在于實時的dom中的方法

    本文主要介紹了javascript判斷元素存在和判斷元素存在于實時的dom中的方法。具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • JavaScript web表單功能交流干貨滿滿

    JavaScript web表單功能交流干貨滿滿

    表單通常用來收集網(wǎng)頁訪問者信息,常見的表單比如搜索引擎的搜索框、各網(wǎng)頁應用的注冊或者登陸界面等。一個表單通常包括多個表單控件
    2021-10-10

最新評論