Raphael.JS 요소를 통해 HTML 정보 업데이트

마우리시오 산체스

일부 HTML 태그와 함께 Raphael.js를 사용하여 클릭 이벤트를 통해 생성되는 시리즈 또는 서클의 정보를 업데이트하려고합니다. 생성 될 때 생성 된 html은 X, Y 및 반경의 위치와 함께 원의 정보를 유지합니다. 원을 이동하거나 크기를 변경할 때 동적으로 변경 될 수 있습니다.이 JSFiddle은이를 더 명확하게 보여줍니다.

http://jsfiddle.net/mauricioSanchez/L4mAL/

내 이벤트 리스너가 있습니다.

    canvasHandler.mouseup(function (e) {

    var mouseX = e.offsetX;
    var mouseY = e.offsetY;
    if (circlePickerSelector) {
        makeCircle(mouseX, mouseY);
        circlePickerSelector = false;
    }       
});

circlePicker.click(function () {
    circlePickerSelector = !circlePickerSelector;

});

이것은 첫 번째 eventListener에서 호출되는 주요 함수입니다. 이를 위해 원과 HTML 요소를 생성합니다.

function makeCircle(mouseX, mouseY) {
    //We call it before our circles are dragged so that their array is waiting to store the information
    // addingArrays(circleCounter);
    var radius;
    var fill;
    var circle = canvas.circle(mouseX, mouseY, 50).attr({
        fill: "hsb(.8, 1, 1)",
        stroke: "none",
        opacity: .5,
    });
    // console.log(circles);
    // We add an ID and a class to the circle
    var ourCircle = $("circle").last();
    ourCircle.attr("class", circleCounter);
    // And then finally push it to our array of circles
    circles.push(circle);
    var handlerPos = [mouseX + 35, mouseY + 35];
    var s = canvas.circle(handlerPos[0], handlerPos[1], 10).attr({
        fill: "hsb(.8, .5, .5)",
        stroke: "none",
        opacity: .5
    });
    //We add an id and a class to our little circle.
    s.node.id = sizerCounter;
    var sizerClass = $('circle').last();
    sizerClass.attr("class", "main-circle sizer");
    var newSizerClass = $(".sizer");
    // console.log(s);
    s.hide();
    //We now assign a handler for each little circle added and a main circle in order to hide them
    var circleClass = $("." + String(circleCounter));
    var sizerID = $("#" + String(sizerCounter));
    circleClass.mouseenter(function () {
        sizerID.toggle();
    });
    circleClass.mouseleave(function () {
        sizerID.hide();
    });
    sizerID.mouseenter(function () {
        $(this).toggle();
    });
    sizerID.mouseleave(function () {
        $(this).hide();
    });
    // console.log(circleClass);
    //We add some resizing and dragging properties
    var start = function () {
        //storing original coordinates
        this.ox = this.attr("cx");
        this.oy = this.attr("cy");
        this.sizer.ox = this.sizer.attr("cx");
        this.sizer.oy = this.sizer.attr("cy")
        this.attr({
            opacity: 1
        });
        this.sizer.attr({
            opacity: 1
        });
    }, move = function (dx, dy) {
            // move will be called with dx and dy
            this.attr({
                cx: this.ox + dx,
                cy: this.oy + dy
            });
            this.sizer.attr({
                cx: this.sizer.ox + dx,
                cy: this.sizer.oy + dy
            });
            //This is the key function to change 
            updateModel(this.attrs.cx, this.attrs.cy, this.node.className.baseVal, this.attrs.r);
        }, up = function () {
            // restoring state
            this.attr({
                opacity: .5
            });
            this.sizer.attr({
                opacity: .5
            });
        }, rstart = function () {
            // storing original coordinates
            this.ox = this.attr("cx");
            this.oy = this.attr("cy");
            this.big.or = this.big.attr("r");
        }, rmove = function (dx, dy) {
            // move will be called with dx and dy
            this.attr({
                cx: this.ox + dy,
                cy: this.oy + dy
            });
            this.big.attr({
                r: this.big.or + (dy < 0 ? -1 : 1) * Math.sqrt(2 * dy * dy)
            });
            updateModel(this.attrs.cx, this.attrs.cy, this.node.className.baseVal, this.attrs.r);
        };
    circle.drag(move, start, up);
    circle.sizer = s;
    s.drag(rmove, rstart);
    s.big = circle;
    //Here we create
    var myCodeList = $(".code-list");
    var htmlString = "<li class='" + circleCounter + "'> <span class='circle-color'> var color = <div class='circle-color-input' contentEditable autocorrect='off'> type a color</div> ; </span> <br> <span class='circle-radius'> This is a test </span> <br> <span class='circle'> This is a test </span> </li>";
    myCodeList.append(htmlString);
    updateList();
    circleCounter++;
    sizerCounter++;     
}

마지막으로이 두 함수를 사용하면 html과 원의 위치를 ​​업데이트 할 수 있습니다.

function updateModel(x, y, _class, r) {
    var len = circles.length;
    for (var i = 0; i < len; i++) {
        if (circles[i].node.className.baseVal == _class) {
            circles[i].attrs.cx = x;
            circles[i].attrs.cy = y;
            circles[i].attrs.r = r;
        }
    }
    updateList();

}

function updateList() {
    //To change that one I have put a class or an id
    var listItems = $('.code-list').find('li.' + circleCounter);
    // console.log(listItems);
    var len = circles.length;
    for (var i = 0; i < circles.length; i++) {
        //We create one reference. This makes looking for one element more effective. Unless we need to search for a particular element
        var currentItem = circles[i];
        var updateStringRadius = "var radius = " + parseInt(currentItem.attrs.r) + ";";
        var updateStringCircle = "circle (" + currentItem.attrs.cx + " ," + currentItem.attrs.cy + ", radius)";
        //This is the div Item for the particular div of each element
        var divItem = $(listItems[i]);
        var radiusItem = divItem.find("span.circle-radius");
        var circleItem = divItem.find("span.circle");
        // console.log(currentItem.attrs.cx);
        radiusItem.text(updateStringRadius);
        console.log($('.circle-radius').html());
        circleItem.text(updateStringCircle);
        // divItem.text(updateString);
        // console.log(divItem);
    }
}

예를 들어 JSfiddle을 살펴보고 세 개의 원을 만듭니다. 원이 생성 된 후 첫 번째 정보 만 업데이트되지만 이제 이동하거나 크기를 조정할 때 변경되지 않습니다.

마우리시오 산체스

나는 그것을 알아. 그래서 문제는이 함수에있었습니다.

function updateList() {
//To change that one I have put a class or an id
var listItems = $('.code-list').find('li.' + circleCounter);
// console.log(listItems);
var len = circles.length;
for (var i = 0; i < circles.length; i++) {
    //We create one reference. This makes looking for one element more effective. Unless we need to search for a particular element
    var currentItem = circles[i];
    var updateStringRadius = "var radius = " + parseInt(currentItem.attrs.r) + ";";
    var updateStringCircle = "circle (" + currentItem.attrs.cx + " ," + currentItem.attrs.cy + ", radius)";
    //This is the div Item for the particular div of each element
    var divItem = $(listItems[i]);
    var radiusItem = divItem.find("span.circle-radius");
    var circleItem = divItem.find("span.circle");
    // console.log(currentItem.attrs.cx);
    radiusItem.text(updateStringRadius);
    console.log($('.circle-radius').html());
    circleItem.text(updateStringCircle);
    // divItem.text(updateString);
    // console.log(divItem);
}

}

핸들러를 시작하고 이에 대한 자식을 찾는 방식을 변경했습니다.

function updateList(){     
 var len = circles.length;
 for (var i = 0; i < circles.length; i++) {
  var currentItem = circles[i];
  var updateStringRadius = "var radius = " + parseInt(currentItem.attrs.r) + ";";
  var updateStringCircle = "circle (" + currentItem.attrs.cx + " ," + currentItem.attrs.cy + ", radius)";
  var divItem = $('.code-list').find('li.circle-' + (i+1))
  var radiusItem = divItem.find("span.circle-radius");
  var circleItem = divItem.find("span.circle");
  radiusItem.text(updateStringRadius);
  circleItem.text(updateStringCircle);
}

요소를 제대로 반복하지 않았습니다. 또한 모범 사례를 위해 class = "1"에서 class = "circle-1"로 클래스를 변경하는 것이 좋습니다.

다음은 JSfiddle입니다.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Raphael.js 텍스트 요소에 추가 데이터 추가

분류에서Dev

onClick 이벤트를 통해 특정 구성 요소 열기-REACT.JS

분류에서Dev

raphael.js를 사용하여 socket.io를 통해 전송 된 매개 변수로 이미지 그리기

분류에서Dev

Raphael.JS : Raphael Paper에서 이미지 DOM 요소를 드래그 앤 드롭하는 방법

분류에서Dev

지시문을 통해 HTML 요소의 가시성 업데이트

분류에서Dev

서블릿 및 JSP를 통해 HTML 테이블 업데이트

분류에서Dev

VBA를 통해 HTML 페이지에서 정보 얻기

분류에서Dev

업데이트보기를 통해 Django 업데이트 사용자

분류에서Dev

Mongoose.js를 통해 MongoDB의 날짜 업데이트 문제

분류에서Dev

ExtJS 그리드 저장소-CodeIgniter를 통해 "저장"/ "업데이트"시 MySQL에 정보 쓰기

분류에서Dev

Selenium VBA를 통해 일부 HTML 요소에 텍스트를 보내는 방법은 무엇입니까?

분류에서Dev

Node JS & Socket.io Advice-경로를 통해 또는 소켓을 통해 데이터베이스에 정보를 게시하는 것이 더 낫습니까?

분류에서Dev

JS를 통해 HTML 원시 DIV 텍스트에서 div 데이터 추출

분류에서Dev

JS를 통해 HTML 원시 DIV 텍스트에서 div 데이터 추출

분류에서Dev

props를 통해 부모에게 보낸 자식 구성 요소의 데이터를 어떻게 업데이트해야합니까?

분류에서Dev

구성 요소를 통해 데이터 보내기

분류에서Dev

Blazor 앱에서 특정 HTML 요소를 수동으로 업데이트

분류에서Dev

Javascript를 통해 특정 스타일로 Html 요소 숨기기

분류에서Dev

모든 div 및 업데이트 요소를 통해 루프

분류에서Dev

모든 div 및 업데이트 요소를 통해 루프

분류에서Dev

모든 div 및 업데이트 요소를 통해 루프

분류에서Dev

메타 데이터의 정보를 통해 스트라이프 구독 취소

분류에서Dev

AJAX에 의해 이미 업데이트 된 AJAX를 통해 값 보내기

분류에서Dev

Rails : link_to를 통해 데이터 업데이트 (보기 없음)

분류에서Dev

jQuery를 통해 최상위 요소에 대한 보호없이 전체 HTML 문서 가져 오기

분류에서Dev

자바 스크립트를 통해 동적으로 생성 된 HTML 페이지에 정보 보내기

분류에서Dev

Jquery를 통해 HTML 요소의 텍스트 얻기

분류에서Dev

Java의 모든 HTML 요소를 통해 반복 (Selenium 테스트)

분류에서Dev

.html 양식 정보가 .php를 통해 이메일로 제출하지 못함

Related 관련 기사

  1. 1

    Raphael.js 텍스트 요소에 추가 데이터 추가

  2. 2

    onClick 이벤트를 통해 특정 구성 요소 열기-REACT.JS

  3. 3

    raphael.js를 사용하여 socket.io를 통해 전송 된 매개 변수로 이미지 그리기

  4. 4

    Raphael.JS : Raphael Paper에서 이미지 DOM 요소를 드래그 앤 드롭하는 방법

  5. 5

    지시문을 통해 HTML 요소의 가시성 업데이트

  6. 6

    서블릿 및 JSP를 통해 HTML 테이블 업데이트

  7. 7

    VBA를 통해 HTML 페이지에서 정보 얻기

  8. 8

    업데이트보기를 통해 Django 업데이트 사용자

  9. 9

    Mongoose.js를 통해 MongoDB의 날짜 업데이트 문제

  10. 10

    ExtJS 그리드 저장소-CodeIgniter를 통해 "저장"/ "업데이트"시 MySQL에 정보 쓰기

  11. 11

    Selenium VBA를 통해 일부 HTML 요소에 텍스트를 보내는 방법은 무엇입니까?

  12. 12

    Node JS & Socket.io Advice-경로를 통해 또는 소켓을 통해 데이터베이스에 정보를 게시하는 것이 더 낫습니까?

  13. 13

    JS를 통해 HTML 원시 DIV 텍스트에서 div 데이터 추출

  14. 14

    JS를 통해 HTML 원시 DIV 텍스트에서 div 데이터 추출

  15. 15

    props를 통해 부모에게 보낸 자식 구성 요소의 데이터를 어떻게 업데이트해야합니까?

  16. 16

    구성 요소를 통해 데이터 보내기

  17. 17

    Blazor 앱에서 특정 HTML 요소를 수동으로 업데이트

  18. 18

    Javascript를 통해 특정 스타일로 Html 요소 숨기기

  19. 19

    모든 div 및 업데이트 요소를 통해 루프

  20. 20

    모든 div 및 업데이트 요소를 통해 루프

  21. 21

    모든 div 및 업데이트 요소를 통해 루프

  22. 22

    메타 데이터의 정보를 통해 스트라이프 구독 취소

  23. 23

    AJAX에 의해 이미 업데이트 된 AJAX를 통해 값 보내기

  24. 24

    Rails : link_to를 통해 데이터 업데이트 (보기 없음)

  25. 25

    jQuery를 통해 최상위 요소에 대한 보호없이 전체 HTML 문서 가져 오기

  26. 26

    자바 스크립트를 통해 동적으로 생성 된 HTML 페이지에 정보 보내기

  27. 27

    Jquery를 통해 HTML 요소의 텍스트 얻기

  28. 28

    Java의 모든 HTML 요소를 통해 반복 (Selenium 테스트)

  29. 29

    .html 양식 정보가 .php를 통해 이메일로 제출하지 못함

뜨겁다태그

보관