lib > photoeditor 0.0.3

HTML5を使った画像エディター 機能はリサイズ、回転、フレーム(4pxの白色パディング、1pxの銀ボーダー)、切り取り(オリジナル画像上をドラッグで範囲指定。cで切り取り実行)。

by s_hiroshi

HTML

<h1>Photo</h1>
<div id="tool-container">
    <div id="drop">File Drop Area</div>
    <div class="panel">
        オリジナル画像の情報<br>
        横のサイズ: <input id="org-width" type="text" size="5" disabled="disabled">&nbsp;
        縦のサイズ: <input id="org-height" type="text" size="5" disabled="disabled">
    </div><!-- /panel -->
    <div class="panel">
        リサイズ<br>
        横 <input id="width" size="5" type="text"> px&nbsp;
        縦 <input type="text" id="height" size="5"> px<br>
        比率固定&nbsp;<input id="ratio" type="checkbox" name="ratio"><br>
        <button id="resize">リサイズ</button>
    </div><!-- /panel -->
    <div class="panel">
        <input id="degree" type="text" size="5"> 度 <button id="rotate">回転</button>
    </div><!-- /panel -->
    <div class="panel">
        <button id="frame">フレームを付ける</button>
    </div><!-- /panel -->
    <div class="panel">
        切り取り(オリジナルへの操作)<br>
        ドラッグで範囲指定。
        cで切り取り。
    </div><!-- /panel -->
    <div class="panel">
        <button id="replace">オリジナルに設定</button>
    </div><!-- /panel -->
</div><!-- /tool-container -->
<div id="image-container">
    <div id="original">
        <h3>オリジナル画像</h3>
        <div id="rectangle"></div>
    </div>
    <div id="canvas-container">
        <h3>編集後の画像</h3>
    </div>
</div>

CSS

@charset "utf-8";
/**
 * Copyright 2012 Sawai Hiroshi
 * http://www.info-town.jp
 *
 */

/**
font size
10    77
11    85
12    93 (was 93)
13    100
14    108 (was 107)
15    116 (was 114)
16    123.1 (was 122)
17    131 (was 129)
18    138.5 (was 136)
19    146.5 (was 144)
20    153.9 (was 152)
21    161.6 (was 159)
22    167
23    174
24    182
25    189
26    197
*/

/* -------------------------------------------------------------------
body
 ------------------------------------------------------------------- */
body {
    line-height: 1.5;
}
button {
    width: 140px;
}
/* -------------------------------------------------------------------
tool area
 ------------------------------------------------------------------- */
#tool-container {
    float: left;
    width: 28%;
    padding: 3px;
    margin-bottom: 10px;
    background: #DDD;
    border: 1px solid #BBB;
}
.panel {
    padding: 8px;
    margin: 3px;
    background: #EFEFEF;
}
#org-width, #org-height {
    background: #F4F4F4;
}
#drop {
    height: 100px;
    padding: 15px 8px;
    margin-bottom: 10px;
    background: #EFEFEF;
    border: 5px dotted #DDD;
}
/* -------------------------------------------------------------------
image area
 ------------------------------------------------------------------- */
#image-container {
    overflow: hidden;
    float: right;
    width: 70%;
}
#image-container h3 {
    margin: 10px;
}
#original {
    float: left;
    width: 45%;
    position: relative;
    overflow: auto;
}
#rectangle {
    z-index: 2;
    position: absolute;
    display: none;
    top: 0;
    left: 0;
    width: 10px;
    height: 10px;
    border: 5px solid #999;
}
#canvas-container {
    overflow: auto;
    width: 45%;
    float: right;
}

JavaScript

/**
 * photoeditor - v0.0.3 beta - 2012-05-21
 * http://www.info-town.jp
 *
 * Copyright (c) 2012 Sawai Hiroshi 
 */

// referred to the following site. 
// http://www.programmingmat.jp/webhtml_lab/canvas_image.html
// http://www.programmingmat.jp/webhtml_lab/canvas_putdata.html
// http://atomicrobotdesign.com/blog/javascript/draw-a-rectangle-using-the-mouse-on-the-canvas-in-less-than-40-lines-of-javascript/
// http://himaxoff.blog111.fc2.com/blog-entry-87.html
// utils
if (!Object.create) {
    (function() {
        function F() {}

        Object.create = function(object) {
            F.prototype = object;
            return new F();
        };
    }());
}



// photoeditor
jQuery(function($) {

    // photoeditor aplication top level object
    var photoeditor = {};

    (function() {

        var image, canvas, cxt, rect = {},
            drag = false;


        /**
         * exception
         */

        function appError(error) {
            alert(error.message);
        }




        /**
         * file open when file drag canvas
         */

        // drag handler
        // cancel default browser event
        $('#drop').get(0).ondragover = function(event) {
            return false;
        };

        // drop handler
        // read file when file drag&drop to canvas
        $('#drop').get(0).ondrop = function(event) {
            var file, reader;

            if (!event.dataTransfer.files.length) {
                return false;
            }

            file = event.dataTransfer.files[0];
            if (!/^image\/(png|jpeg|gif)$/.test(file.type)) {
                appError({
                    message: 'file type error when file drag open'
                });
                return false;
            }

            reader = new FileReader();

            reader.onload = function() {
                image = document.createElement('img');
                image.setAttribute('src', reader.result);
                image.draggable = false;
         ...