2017-04-05 49 views
0

interactjsを使用してイメージを回転、サイズ変更、ドラッグしようとしています。
ピンチを使用してサイズを変更しています。Interactjs rotationは位置を移動します

画像を回転させた後、ドラッグ機能はうまく動作せず、画像を間違った座標に配置します。
(幅/ 2、高さ/ 2)で位置を保持するように回転する前にimgを移動しようとしましたが、これによってサイズ変更と回転機能が停止しました。
ここに私が使用しているコードがあります。ズームだけ回転させる

ピンチは、タッチ携帯電話上で動作します:

Hereは(ピンチ・ツー・ズーム、回転のために電話で確認してください)フィドルです。

HTML

<div style="display:flex;flex-direction:row;border:solid grey 2px;border-radius:20px;position:relative;float:left;background-color:silver;width:100%"> 

    <img class="Choice" src="http://gifmemorecreativity.com/images/design/small/2014-12-02/19220-50b6c96d8aa44ed1511a962bae279f25.png" style="width:150px; height:150px;" /> 
    <img class="Choice" src="http://gifmemorecreativity.com/images/design/small/2014-04-27/5895-37981e6fff910eef9907adaf99faa6b6.png" style="width:150px; height:150px;" /> 
    <img class="Choice" src="http://gifmemorecreativity.com/images/design/small/2014-04-27/5728-08fc043472bfa4cc9ba6d7c4a90324e0.png" style="width:150px; height:150px;" /> 
    <img class="Choice" src="http://gifmemorecreativity.com/images/design/small/2014-11-30/17784-d2820ac7614e8f4eeb755c38bdccadc0.png" style="width:150px; height:150px;" /> 
    <img class="Choice" src="http://gifmemorecreativity.com/images/design/small/2014-04-27/5728-08fc043472bfa4cc9ba6d7c4a90324e0.png" style="width:150px; height:150px;" /> 
    <img class="Choice" src="http://gifmemorecreativity.com/images/design/small/2014-04-27/5895-37981e6fff910eef9907adaf99faa6b6.png" style="width:150px; height:150px;" /> 
</div> 
<div style="display:flex;flex-direction:row;justify-content:space-between;align-items:center;border:solid grey 2px;border-radius:20px;position:relative;float:left;background-color:silver;height:50px;width:150px"> 
    <button id="save" style="width:50px" /> 
    <button id="savetwo" style="height:40px;border-radius:20px;width:70px">Save Image</button> 
</div> 
<br /> 
</div> 
</div> 

SCRIPT

$(document).ready(function(){ 
    $(".Choice").click(function() { 

    console.log($(this)); 
    var url = '"' + $(this)[0].src + '"'; 
    var index = url.indexOf("http://"); 
    console.log(url); 
    var modurl = url.substring(index, url.length - 1); 
    console.log(url); 
    $(".ChoiceImage").attr("src", modurl);//url appends src to the current uri so I had to split it(suggest alternative) 
    }); 

    var scale = 1,angle=0; 
    var gestureArea = document.getElementById('gesture-area'); 
    var scaleElement = document.getElementById('chosenOne'); 
    var scaleElementParent = scaleElement.parentElement; 

    interact(gestureArea).gesturable({ 
    onstart: function(event) { 

    }, 
    onmove: function(event) { 

    angle += event.da; 

    scale = scale * (1 + event.ds); 

    scaleElement.style.webkitTransform = 
     scaleElement.style.transform = 
     'scale(' + scale + ') rotate('+ angle + 'deg)'; 

    scaleElementParent.style.webkitTransform = 
     scaleElementParent.style.transform = 
     'scale(' + scale + ') rotate('+ angle + 'deg)'; 

    dragMoveListener(event); 
    }, 
    onend: function(event) { 

    } 
    }).draggable({ onmove: dragMoveListener }); 

    function dragMoveListener(event) { 

    var target = event.target.children[0]; 
    console.log(target); 

    // keep the dragged position in the data-x/data-y attributes 
    x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx, 
    y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy; 

    // translate the element 
    target.style.webkitTransform = 
     target.style.transform = 
     'translate(' + x + 'px, ' + y + 'px)'; 

    // update the posiion attributes 
    target.setAttribute('data-x', x); 
    target.setAttribute('data-y', y); 
    } 

    // this is used later in the resizing and gesture demos 
    window.dragMoveListener = dragMoveListener; 

}); 

ユーザがアクションを実行するために画像の一覧から画像をクリックします。あなたの主な問題は、画像「ジェスチャーエリアを拡大して回転させるようにした、事前に

おかげ

答えて

1

ジェスチャ検出要素が回転しているのであれば、interac.jsだけで動きを誤って解釈することができます。

コードを少しきれいにしました。ボタンのように、この回答から未使用の要素をすべて削除しました...

ここにコードは完全にd CodePenをモバイルデバイスで試用してください。

console.clear(); 
 
$(".Choice").click(function() { 
 
    $(".ChoiceImage").attr("src", $(this).attr("src")); 
 
}); 
 

 
var scale = 1, 
 
    angle=0, 
 
    gestureArea = $('#gestureArea')[0], 
 
    scaleElement = $('.ChoiceImage'), 
 
    scaleElementParent = $('#gestureArea'); 
 

 

 
// Scale and rotate 
 
interact(gestureArea).gesturable({ 
 
    onstart: function(event) { 
 
    
 
    }, 
 
    onmove: function(event) { 
 

 
    angle += event.da; 
 
    scale = scale * (1+event.ds); 
 

 
    scaleElement.css({'transform':'scale(' + scale + ') rotate('+ angle + 'deg)'}); 
 
    //scaleElementParent.css({'transform':'scale(' + scale + ') rotate('+ angle + 'deg)'}); 
 

 
    dragMoveListener(event); 
 
    }, 
 
    onend: function(event) { 
 
    
 
    } 
 
}).draggable({ onmove: dragMoveListener }); 
 

 

 
// Drag 
 
function dragMoveListener(event) { 
 

 
    var target = $(event.target); 
 

 
    // keep the dragged position in the data-x/data-y attributes 
 
    x = (parseFloat(target.attr('data-x')) || 0) + event.dx; 
 
    y = (parseFloat(target.attr('data-y')) || 0) + event.dy; 
 

 
    // translate the element 
 
    target.css({'transform':'translate(' + x + 'px, ' + y + 'px)'}); 
 

 
    // update the posiion attributes 
 
    target.attr('data-x', x); 
 
    target.attr('data-y', y); 
 
} 
 

 
// this is used later in the resizing and gesture demos 
 
window.dragMoveListener = dragMoveListener;
#gestureArea{ 
 
    position:relative; 
 
    z-index:999; 
 
    width:150px; 
 
    height:150px; 
 
    background-color:transparent; 
 
    border:none; 
 
    padding: 0px 0px 0px 0px; 
 
} 
 
.container{ 
 
    display:flex; 
 
    flex-direction:row; 
 
    border:solid grey 2px; 
 
    border-radius:20px; 
 
    position:relative; 
 
    float:left; 
 
    background-color:silver; 
 
    width:100%; 
 
    position:fixed; 
 
    bottom:0; 
 
} 
 
img{ 
 
    width:150px; 
 
    height:150px; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/interact.js/1.2.8/interact.min.js"></script> 
 

 
<div id="gestureArea"> 
 
    <img id="chosenOne" class="ChoiceImage resize-drag"> 
 
</div> 
 

 
<div class="container"> 
 
    <img class="Choice" src="http://gifmemorecreativity.com/images/design/small/2014-12-02/19220-50b6c96d8aa44ed1511a962bae279f25.png"> 
 
</div>

関連する問題