Skip to content

Instantly share code, notes, and snippets.

@3846masa
Last active August 29, 2015 14:13
Show Gist options
  • Save 3846masa/841e3d8944f51c36a014 to your computer and use it in GitHub Desktop.
Save 3846masa/841e3d8944f51c36a014 to your computer and use it in GitHub Desktop.
コンソールに無理やり画像を出力する
(function(){
console.drawImage = function (img) {
var refresh = true, maxWidth;
if (arguments.length >= 2) {
if (typeof arguments[1] === "boolean") refresh = arguments[1];
else if (typeof arguments[1] === "number") maxWidth = arguments[1];
if (arguments.length >= 3 && typeof arguments[2] === "boolean") refresh = arguments[2];
}
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
img.width = img.width || img.videoWidth;
img.height = img.height || img.videoHeight;
var width = canvas.width = maxWidth || img.width;
var zoom = width / img.width;
var height = canvas.height = img.height * zoom;
ctx.drawImage(img, 0, 0, img.width, img.height, 0, 0, width, height);
var pixels = ctx.getImageData(0, 0, width, height);
var data = Array.prototype.slice.apply(pixels.data);
var text = '', cssList = [];
for (var y = 0; y < height - 1; y++) {
for (var x = 0; x < width - 1; x++) {
var n = x * 4 + y * width * 4;
var css = 'background:rgba(' + data.slice(n, n + 4).join(',') + ')';
text += "%c\x20\x20";
cssList.push(css);
}
text += "\n%c\x20"; cssList.push('background:transparent');
}
var args = [text].concat(cssList);
if (refresh) console.clear();
console.log.apply(console, args);
};
})();

drawImageToConsole.js

LICENSE

Copyright (c) 2015 3846masa

Released under the MIT License.

Usage

console.drawImage(img, [maxWidth], [refresh])

Parameters

Name Type Description
img HTMLElement 画像のHTML要素
maxWidth int (optional) 出力画像の横幅
refresh boolean (default:true) コンソールを初期化するか
The MIT License (MIT)
Copyright (c) 2015 3846masa
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