위토즈 - 개발자프로그램판매공간

정보공유

[HTML5] canvas 사각툴 크키 조정 예제

 

 

var canvas = document.getElementById('canvas'),

    ctx = canvas.getContext('2d'),

    rect = {},

    drag = false,

    mouseX,

    mouseY,

    closeEnough = 10,

    dragTL = dragBL = dragTR = dragBR = false;

 

function init() {

    canvas.addEventListener('mousedown', mouseDown, false);

    canvas.addEventListener('mouseup', mouseUp, false);

    canvas.addEventListener('mousemove', mouseMove, false);

 

    rect = {

        startX: 100,

        startY: 200,

        w: 300,

        h: 200

    }

}

 

function mouseDown(e) {

    mouseX = e.pageX - this.offsetLeft;

    mouseY = e.pageY - this.offsetTop;

 

    // if there isn't a rect yet

    if (rect.w === undefined) {

        rect.startX = mouseY;

        rect.startY = mouseX;

        dragBR = true;

    }

 

    // if there is, check which corner

    //   (if any) was clicked

    //

    // 4 cases:

    // 1. top left

    else if (checkCloseEnough(mouseX, rect.startX) && checkCloseEnough(mouseY, rect.startY)) {

        dragTL = true;

    }

    // 2. top right

    else if (checkCloseEnough(mouseX, rect.startX + rect.w) && checkCloseEnough(mouseY, rect.startY)) {

        dragTR = true;

 

    }

    // 3. bottom left

    else if (checkCloseEnough(mouseX, rect.startX) && checkCloseEnough(mouseY, rect.startY + rect.h)) {

        dragBL = true;

 

    }

    // 4. bottom right

    else if (checkCloseEnough(mouseX, rect.startX + rect.w) && checkCloseEnough(mouseY, rect.startY + rect.h)) {

        dragBR = true;

 

    }

    // (5.) none of them

    else {

        // handle not resizing

    }

 

    ctx.clearRect(0, 0, canvas.width, canvas.height);

    draw();

 

}

 

function checkCloseEnough(p1, p2) {

    return Math.abs(p1 - p2) < closeEnough;

}

 

function mouseUp() {

    dragTL = dragTR = dragBL = dragBR = false;

}

 

function mouseMove(e) {

    mouseX = e.pageX - this.offsetLeft;

    mouseY = e.pageY - this.offsetTop;

    if (dragTL) {

        rect.w += rect.startX - mouseX;

        rect.h += rect.startY - mouseY;

        rect.startX = mouseX;

        rect.startY = mouseY;

    } else if (dragTR) {

        rect.w = Math.abs(rect.startX - mouseX);

        rect.h += rect.startY - mouseY;

        rect.startY = mouseY;

    } else if (dragBL) {

        rect.w += rect.startX - mouseX;

        rect.h = Math.abs(rect.startY - mouseY);

        rect.startX = mouseX;

    } else if (dragBR) {

        rect.w = Math.abs(rect.startX - mouseX);

        rect.h = Math.abs(rect.startY - mouseY);

    }

    ctx.clearRect(0, 0, canvas.width, canvas.height);

    draw();

}

 

function draw() {

    ctx.fillStyle = "#222222";

    ctx.fillRect(rect.startX, rect.startY, rect.w, rect.h);

    drawHandles();

}

 

function drawCircle(x, y, radius) {

    ctx.fillStyle = "#FF0000";

    ctx.beginPath();

    ctx.arc(x, y, radius, 0, 2 * Math.PI);

    ctx.fill();

}

 

function drawHandles() {

    drawCircle(rect.startX, rect.startY, closeEnough);

    drawCircle(rect.startX + rect.w, rect.startY, closeEnough);

    drawCircle(rect.startX + rect.w, rect.startY + rect.h, closeEnough);

    drawCircle(rect.startX, rect.startY + rect.h, closeEnough);

}

 

init();

 

 

정보공유
Total 438건 21 페이지
번호 제목 글쓴이 날짜 조회
138 vertical 메뉴 슬라이드 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 09-12 25801
137 LMS 문자발송 오류 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 04-18 25779
136 첨부 파일 이전 샘플 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 03-14 25777
135 classic ASP ajax 문자열 처리. 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 04-12 25439
134 저장하고 나서 소스를 수정하지 않았는데 오류가 발생할경우. 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 07-10 25376
133 3자리 은행 코드 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 08-14 25346
132 jquery 를 쓰지않고 ajax 로 데이터값 전송하기. 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 04-05 25288
131 IOS (아이폰) 로 결제 후 쇼핑몰로 돌아가지 못하는 현상 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 06-28 25179
130 크롬에서 결제만 되고 정상적으로 처리가 되지 않는 경우 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 09-14 24922
129 스마트 에디터 기본 편집모드 변경하는 방법 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 04-11 24786
128 문서경로 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 03-27 24704
127 블럭 페이지 샘플 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 07-24 24500
126 상품의 옵션이 너무 많이 등록되어있을경우 페이지 로딩이 지연됨. 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 07-20 24418
125 htaccess 를 이용한 도메인 처리 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 04-09 24391
열람중 canvas 사각툴 크키 조정 예제 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 05-01 24336