Skip to content

Instantly share code, notes, and snippets.

@s-hiroshi
Last active April 10, 2016 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s-hiroshi/4050946 to your computer and use it in GitHub Desktop.
Save s-hiroshi/4050946 to your computer and use it in GitHub Desktop.
JavaScript > ドラッグアンドドロップで画像を表示

License

Copyright (c) 2016 Hiroshi Sawai

Licensed under the MIT license, see LICENSE.txt.

@charset "utf-8";
body {
line-height: 1.5;
}
.row {
margin-bottom: 2em;
}
.btn {
width: 170px;
}
.view {
position: relative;
height: 300px;
border: 2px dotted #E4E4E4;
}
/**
* canvas画像オブジェクト
*
* 画像をキャンパスへ描画する基本的な流れ
* <ul>
* <li>img要素を作成(ノードには追加しない)
* <li>img要素に画像を読込
* <li>img要素をcanvasへ描画
* </ul>
*
* @author Hiroshi Sawai <info@info-town.jp>
*/
jQuery(function($) {
var inputImg;
var inputCanvas = $('<canvas>').appendTo($('#input-view')).get(0);
var outputCanvas = $('<canvas>').appendTo($('#output-view')).get(0);
var inputCxt;
var outputCxt;
// エラー表示
function alert(text) {
window.alert(text);
}
// img要素が存在すればtrue。存在しなければアラートを表示しfalseを返す。
function checkImage () {
if (($(inputImg).length > 0) === false ) {
alert('画像がありません。');
return false;
}
return true;
}
// 読込画像タイプの確認
// 適切な画像タイプならばtrue。対応していないタイプならばアラートを表示してfalseを返す
function checkFileType(text) {
// ファイルタイプの確認
if (text.match(/^image\/(png|jpeg|gif)$/) === null) {
alert('対応していないファイル形式です。\nファイルはPNG, JPEG, GIFに対応しています。');
return false;
}
return true;
}
/*
* 画像表示処理
*/
// 画像読込ハンドラ
function read(reader) {
return function() {
// imgへオブジェクトを読み込む
inputImg = $('<img>').get(0);
inputImg.onload = function() {
try {
inputCanvas.width = inputImg.width;
inputCanvas.height = inputImg.height;
inputCxt.clearRect(0, 0, inputCanvas.width, inputCanvas.height);
inputCxt.drawImage(inputImg, 0, 0, inputImg.width, inputImg.height);
} catch (e) {
alert('画像を開けませんでした。');
}
};
inputImg.setAttribute('src', reader.result);
};
}
// 参照ボタンを使う読込処理
$('#upload').change (function() {
var file, reader;
// 選択したファイル情報
file = this.files[0];
// ファイルタイプの確認
if (checkFileType(file.type) === false) {
return false;
}
// inputCxt作成
inputCxt = inputCanvas.getContext('2d');
// canvasに描画
reader = new FileReader();
reader.onload = read(reader);
reader.readAsDataURL(file);
});
/*
* ドラッグアンドドロップの読込処理
*/
$('#input-view').get(0).ondragover = function() {
return false;
};
// bind('ondrop', function() {});はうまく動かなかった(2012.11.07)
$('#input-view').get(0).ondrop = function(event) {
var file, reader;
if (event.dataTransfer.files.length === 0) {
alert('画像を開けませんでした。');
return false;
}
// ドロップされたファイル情報
file = event.dataTransfer.files[0];
// ファイルタイプの確認
if (checkFileType(file.type) === false) {
return false;
}
// inputCxt作成
inputCxt = inputCanvas.getContext('2d');
// canvasへの描画
reader = new FileReader();
reader.onload = read(reader);
reader.readAsDataURL(file);
// バブリング・デフォルト処理停止
return false;
};
/*
* 転送
*/
$('#forward').click(function() {
var data = inputCxt.getImageData(0, 0, inputImg.width, inputImg.height);
outputCanvas.width = data.width;
outputCanvas.height = data.height;
outputCxt = outputCanvas.getContext('2d');
outputCxt.putImageData(data, 0, 0);
});
});
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>画像をドラッグドロップでキャンバスへ描画</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" media="all">
<link rel="stylesheet" type="text/css" href="canvas.css" media="screen">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="canvas.js"></script>
</head>
<body>
<div class="container">
<div id="header"><h1>ドラッグアンドドロップで画像描画</h1></div>
<div class="row">
<div class="span12"><input id="upload" name="upload" type="file" value="File Upload"></div>
</div>
<div class="row">
<div class="span12">JPEG,PNgファイルをドラッグアンドドロップで表示できます。</div>
</div>
<div class="row">
<div class="span5">
<div id="input-view" class="view">
</div>
</div><!-- /span5 -->
<div class="span2">
<button id="forward" class="btn">転送 &rArr;</button>
</div><!-- /span2 -->
<div class="span5">
<div id="output-view" class="view"></div>
</div><!-- /span5 -->
</div>
</div><!-- /container -->
</body>
</html>
The MIT License (MIT)
Copyright (c) 2016 Hiroshi Sawai
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment