css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <script
  src="https://code.jquery.com/jquery-3.2.1.min.js"
  integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
  crossorigin="anonymous"></script>
src1: <input id="file1" type="file"><br><br>
src2: <input id="file2" type="file">
<br><br>
<input id="compare" type="button" value="compare">
<br><br>
<div id="left-area">
src1<br>
  <canvas id="src1-canvas"></canvas>
</div>
<div id="center-area">
src2<br>
  <canvas id="src2-canvas"></canvas>
</div>
<div>
compared<br>
  <canvas id="compared-canvas"></canvas>
</div>

            
          
!
            
              body {
  background-color: #fffacd;
}
#left-area, #center-area {
  float: left;
  margin-right: 20px;
}
            
          
!
            
              $(function() { Main.init(); });

var Main = (function() {
  var 
  _srcCtxArray = [],  // 変更前の画像を表示するCanvasのコンテキスト
  _comparedCtx,       // 比較の画像を表するCanvasのコンテキスト
  
  // 初期化メソッド
  _init = function() {    
    _comparedCtx = $('#compared-canvas')[0].getContext('2d');
    _attachEvents();
  },
      
  // イベントハンドラ定義
  _attachEvents = function() {
    
    // ファイル変更時の処理
    $('#file1, #file2').on('change', function(e) {
      var index = parseInt($(this).prop('id').substring('file'.length), 10) - 1;
      
      _loadFile(e.target.files[0]) // ファイルを読み込む
      .then(_loadImage) // 画像をロードする
      .then(function(image) {
        _resizeCanvas(index, image.width, image.height);
        _srcCtxArray[index].drawImage(image, 0, 0);
        _comparedCtx.clearRect(0, 0, _comparedCtx.canvas.width, _comparedCtx.canvas.height);
      });     
    });
    
    // compareボタン押下時の処理
    $('#compare').click(function() {
      var srcImageData = _srcCtxArray[0].getImageData(0, 0, _srcCtxArray[0].canvas.width, _srcCtxArray[0].canvas.height),
          dstImageData = _srcCtxArray[1].getImageData(0, 0, _srcCtxArray[1].canvas.width, _srcCtxArray[1].canvas.height),
          imageData;
      
      // 画像サイズの比較
      if(_srcCtxArray[0].canvas.width !== _srcCtxArray[1].canvas.width
      || _srcCtxArray[0].canvas.height !== _srcCtxArray[1].canvas.height) {
        alert('Different image size');
        return;
      }
      // compared-canvasのリサイズ
      $('#compared-canvas').prop({
        width: _srcCtxArray[0].canvas.width,
        height: _srcCtxArray[0].canvas.height
      });
      _comparedCtx = $('#compared-canvas')[0].getContext('2d');
      imageData = _compare(srcImageData, dstImageData);
      _comparedCtx.clearRect(0, 0, _comparedCtx.canvas.width, _comparedCtx.canvas.height);
      _comparedCtx.putImageData(imageData, 0, 0);
    });
  },
      
  _loadFile = function(file) {
    return new Promise(function(resolve, reject) {
      var reader = new FileReader();
      // dataURL形式でファイルを読み込む
      reader.readAsDataURL(file);      
      reader.onload = function() {
        resolve(reader.result);
      };
    });              
  },
      
  _loadImage = function(dataUrl) {
    return new Promise(function(resolve, reject) {
      var image = new Image();
      image.onload = function() {
        resolve(image);
      };
      image.src = dataUrl;
    });
  },  
      
  // canvasをリサイズする
  _resizeCanvas = function(index, width, height) {    
    // canvasのサイズを変更する
    $('#src' + (index + 1) + '-canvas').prop({ 
      width: width, 
      height: height 
    });
    // コンテキストを取得しなおす
    _srcCtxArray[index] = $('#src' + (index + 1) + '-canvas')[0].getContext('2d');
  },
      
  // imageData作成
	_createImageData = function(width, height) {
    var canvas = document.createElement('canvas'),
        ctx, imageData;
    canvas.width = width;
    canvas.height = height;
    ctx = canvas.getContext('2d');
    imageData = ctx.createImageData(width, height);
    return imageData;
	},
  
  // 画像の比較
  _compare = function(srcImageData, dstImageData) {
             
    var srcData = srcImageData.data,  
      dstData = dstImageData.data,  
      imgWidth = srcImageData.width,
      imgHeight = srcImageData.height,
      imageData = _createImageData(imgWidth, imgHeight),
      data = imageData.data,      
      x, y, pixelIndex;
         
    for(x = 0; x < imgWidth; x += 1) {      
      for(y = 0; y < imgHeight; y += 1) {        
        pixelIndex = (y * imgWidth + x) * 4; // ピクセルのインデックスを取得
        if(!comparePixel(pixelIndex, srcData, dstData)) {// 差異がある -> 赤で塗る
          data[pixelIndex] = 255;
          data[pixelIndex + 1] = 0;
          data[pixelIndex + 2] = 0;
          data[pixelIndex + 3] = 255;
        }       
      }
    }
    
    return imageData;
    
    function comparePixel(pixelIndex, srcData, dstData) {
      return srcData[pixelIndex] === dstData[pixelIndex]
        && srcData[pixelIndex + 1] === dstData[pixelIndex + 1]
        && srcData[pixelIndex + 2] === dstData[pixelIndex + 2]
        && srcData[pixelIndex + 3] === dstData[pixelIndex + 3];        
    }
  };
  
  // _initメソッドのみを公開する
  return {
    init: _init 
  };
  
}());
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console