第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

KonvaJS - 圍繞光標(biāo)旋轉(zhuǎn)矩形而不使用偏移

KonvaJS - 圍繞光標(biāo)旋轉(zhuǎn)矩形而不使用偏移

UYOU 2022-01-13 16:09:11
我正在使用 KonvaJS 將矩形拖放到預(yù)定義的插槽中。一些插槽需要旋轉(zhuǎn) 90 度。我在垂直旋轉(zhuǎn)的插槽周圍有一個(gè)命中框,因此當(dāng)用戶將矩形拖入該區(qū)域時(shí),它將自動(dòng)旋轉(zhuǎn) 90 度(以匹配方向)。當(dāng)它旋轉(zhuǎn)時(shí),它會(huì)從鼠標(biāo)下方移出。這可以通過(guò)偏移來(lái)解決,但是在捕捉后矩形不會(huì)在視覺上與框?qū)R。這可以(可能)通過(guò)附加代碼來(lái)解決。我試圖旋轉(zhuǎn)矩形,然后在鼠標(biāo)下移動(dòng)它。由于用戶仍在拖動(dòng)它,這似乎沒(méi)有按我的計(jì)劃工作。是否可以在不使用偏移的情況下強(qiáng)制矩形在鼠標(biāo)下旋轉(zhuǎn)?這是一個(gè)顯示問(wèn)題的小提琴 - 可以通過(guò)將第一個(gè)變量設(shè)置為 true 來(lái)演示偏移問(wèn)題。 https://jsfiddle.net/ChaseRains/1k0aqs2j/78/var width = window.innerWidth;var height = window.innerHeight;var rectangleLayer = new Konva.Layer();var holdingSlotsLayer = new Konva.Layer();var controlLayer = new Konva.Layer();var stage = new Konva.Stage({  container: 'container',  width: width,  height: height,  draggable: true});//vertical holding spotholdingSlotsLayer.add(new Konva.Rect({  x: 300,  y: 25,  width: 130,  height: 25,  fill: '#fff',  draggable: false,  rotation: 90,  stroke: '#000'}));//horizontal holding spotholdingSlotsLayer.add(new Konva.Rect({  x: 25,  y: 75,  width: 130,  height: 25,  fill: '#fff',  draggable: false,  rotation: 0,  stroke: '#000'}));//mask to set boundaries around where we wannt to flip the rectanglecontrolLayer.add(new Konva.Rect({  x: 215,  y: 15,  width: 150,  height: 150,  fill: '#fff',  draggable: false,  name: 'A',  opacity: 0.5}));stage.add(holdingSlotsLayer, controlLayer);//function for finding intersectionsfunction haveIntersection(placeHolder, rectangle, zone) {  if (rectangle.rotation == 0 || zone == true) {    return !(      rectangle.x > placeHolder.x + placeHolder.width ||      rectangle.x + rectangle.width < placeHolder.x ||      rectangle.y > placeHolder.y + placeHolder.height ||      rectangle.y + rectangle.height < placeHolder.y    );  } else {    return !(      rectangle.x > placeHolder.x + 25 ||      rectangle.x + rectangle.width < placeHolder.x ||      rectangle.y > placeHolder.y + placeHolder.height + 90 ||      rectangle.y + rectangle.height < placeHolder.y    );  }}
查看完整描述

1 回答

?
寶慕林4294392

TA貢獻(xiàn)2021條經(jīng)驗(yàn) 獲得超8個(gè)贊

這是一個(gè)在鼠標(biāo)下旋轉(zhuǎn)矩形的簡(jiǎn)單函數(shù),無(wú)需使用 konva offset()。我使用補(bǔ)間來(lái)應(yīng)用移動(dòng),但如果您更喜歡在沒(méi)有補(bǔ)間的情況下使用它,只需應(yīng)用 rect.rotate() 然后應(yīng)用 newPos x & y 作為位置。


編輯:OP指出,如果您單擊,在矩形完成動(dòng)畫時(shí)按住鼠標(biāo),然后拖動(dòng),則矩形會(huì)跳開。是什么賦予了 ?好吧,當(dāng) mousedown 事件運(yùn)行時(shí),Konva 會(huì)記錄形狀在其內(nèi)部拖動(dòng)功能中的初始位置。然后當(dāng)我們開始實(shí)際拖動(dòng)鼠標(biāo)時(shí),Konva 會(huì)盡職盡責(zé)地在它計(jì)算出的位置重新繪制形狀?,F(xiàn)在,“我們”知道我們將形狀移入了代碼,但我們沒(méi)有讓 Konva 參與我們的詭計(jì)。


解決方法是調(diào)用


 rect.stopDrag(); 

 rect.startDrag();

新位置確定后立即進(jìn)行。因?yàn)槲沂褂玫氖茄a(bǔ)間,所以我在其中一個(gè)補(bǔ)間的 onFinish() 回調(diào)函數(shù)中執(zhí)行此操作 - 如果您應(yīng)用多個(gè)補(bǔ)間,您將希望確保它是最終補(bǔ)間。我僥幸逃脫,因?yàn)槲业难a(bǔ)間在同一時(shí)期運(yùn)行。如果您不使用補(bǔ)間,只需在對(duì)形狀應(yīng)用最后一個(gè) rotate() 或 position() 調(diào)用時(shí)立即調(diào)用上述方法。


function rotateUnderMouse(){



  // Get the stage position of the mouse

  var mousePos = stage.getPointerPosition();


  // get the stage position of the mouse

  var shapePos = rect.position();


  // compute the vector for the difference

  var rel = {x: mousePos.x - shapePos.x, y: mousePos.y - shapePos.y} 


  // Now apply the rotation

  angle = angle + 90;



  // and reposition the shape to keep the same point in the shape under the mouse 

  var newPos = ({x: mousePos.x  + rel.y , y: mousePos.y - rel.x}) 


  // Just for fun, a tween to apply the move: See https://konvajs.org/docs/tweens/Linear_Easing.html

  var tween1 = new Konva.Tween({

    node: rect,

    duration: 0.25,

    x:  newPos.x,

    y: newPos.y,

    easing: Konva.Easings.Linear,

    onFinish: function() { rect.stopDrag(); rect.startDrag();}

  });

  

  // and a tween to apply the rotation 

  tween2 = new Konva.Tween({

    node: rect,

    duration: 0.25,

    rotation: angle,

    easing: Konva.Easings.Linear

  });

    

  

  tween2.play();

  tween1.play();



  

}



function setup() {


// Set up a stage and a shape

stage = new Konva.Stage({

  container: 'canvas-container',

  width: 650,

  height: 300

});



layer = new Konva.Layer();

stage.add(layer);


newPos = {x: 80, y: 40};

rect = new Konva.Rect({

   width: 140, height: 50, x: newPos.x, y: newPos.y, draggable: true, stroke: 'cyan', fill: 'cyan'

  })

layer.add(rect);


stage.draw()


rect.on('mousedown', function(){

  rotateUnderMouse()

})


}


var stage, layer, rect, angle = 0;


setup()

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/konva/4.0.13/konva.js"></script>


<p>Click the rectangle - it will rotate under the mouse.</p>


<div id="canvas-container"></div>


查看完整回答
反對(duì) 回復(fù) 2022-01-13
  • 1 回答
  • 0 關(guān)注
  • 513 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)