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

정보공유

[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 436건 14 페이지
번호 제목 글쓴이 날짜 조회
241 SIR 편리님의 올리신 SNS 로그인 API 설정. 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 06-21 44150
240 화면 하단 TOP 버튼 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 06-14 27958
239 ajax 로 불러오는 html 에 hover 처리를 해야할경우 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 05-31 30136
238 차세대 javascript 컨트롤 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 05-22 41133
237 IE9 이전 버전에서 Canvas 사용가능하도록 하는 js 라이브러리 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 05-22 37794
236 ajax 를 이용한 비동기식 삭제 데이터 처리. 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 05-20 32368
235 Magnific Popup 예제 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 05-19 33274
234 SNS LINK 공유 링크. 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 05-19 47664
233 array radio 버튼 유효성 체크 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 05-16 27346
232 오디오 태그 속성정리 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 05-16 34880
231 반응형 테이블 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 05-15 35992
230 첨부파일 이미지파일만 등록하게 하기 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 05-07 31756
229 스마트 에디터 반응형에서 가로사이즈 맞게 출력시키기 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 05-04 41145
228 부트스트랩 슬라이더 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 05-01 43754
열람중 canvas 사각툴 크키 조정 예제 위토즈쪽지보내기 메일보내기 자기소개 아이디로 검색 전체게시물 05-01 24352