2017-01-17 5 views
0

説明コピーは、私はあなたが画像をアップロードして、上のクリックした後の画像の特定の部分のHEXコードを返すことができるページを作成しているJS

から動的にロードされますそれ。

ユーザーが画像をクリックするたびに、HEXコードがページ上部のボックスに表示されます。

ここでは、クリック可能な16進数コードを示すボックスを作成しようとしています。クリックすると、その値(HEXコード)がクリップボードにコピーされます。

enter image description here

ISSUE

コピー作品ではなく、すべてのボックスについて。画像上で3つの部分をクリックすると、3つのボックスに16進数のコードが表示されます。最後のもの(赤色)をクリックすると、コピーは機能しません。最初のボックスをクリックした場合にのみトリガーされます。

SNIPPET

$(function() { 
 
//Script that hides the input box (label is a substitute) 
 
(function(e,t,n){var r=e.querySelectorAll("html")[0];r.className=r.className.replace(/(^|\s)no-js(\s|$)/,"$1js$2")})(document,window,0); 
 

 
//Hex picker function 
 
var $picked = $("#picked"); // Just to preview picked colors 
 
\t var canvas = $('#canvas_picker')[0]; 
 
\t var context = canvas.getContext('2d'); 
 

 

 
\t $("#file_upload").change(function (e) { 
 
\t var F = this.files[0]; 
 
\t var reader = new FileReader(); 
 
\t reader.onload = imageIsLoaded; 
 
\t reader.readAsDataURL(F); 
 
\t }); 
 

 
$('#fileLabel').bind({ 
 
\t dragover: function(e){ 
 
\t \t e.preventDefault(); 
 
\t \t e.stopPropagation(); 
 
\t }, 
 
\t dragleave: function(e){ 
 
\t \t e.preventDefault(); 
 
\t \t e.stopPropagation(); 
 
\t }, 
 
\t drop: function(e){ 
 
\t \t e.preventDefault(); 
 
\t \t e.stopPropagation(); 
 
\t \t var F = e.originalEvent.dataTransfer.files[0]; 
 
\t \t var reader = new FileReader(); 
 
\t \t reader.onload = imageIsLoaded; 
 
\t \t reader.readAsDataURL(F); 
 
\t } 
 
}); 
 

 
\t function imageIsLoaded(e) { 
 
\t var img = new Image(); 
 
\t img.onload = function(){ 
 
\t \t canvas.width = this.width; 
 
\t \t canvas.height = this.height; 
 
\t \t context.drawImage(this, 0, 0); 
 
\t }; 
 
\t img.src = e.target.result; 
 
\t } 
 

 
\t $('#canvas_picker').click(function(event){ 
 
\t var x = event.pageX - $(this).offset().left; 
 
\t var y = event.pageY - $(this).offset().top; 
 
\t var img_data = context.getImageData(x,y , 1, 1).data; 
 
\t var R = img_data[0]; 
 
\t var G = img_data[1]; 
 
\t var B = img_data[2]; 
 
\t var rgb = R + ',' + G + ',' + B ; 
 
\t var hex = rgbToHex(R,G,B); 
 
\t $('#rgb input').val(rgb); 
 
\t $('#hex input').val('#' + hex); 
 
\t $picked.append("<span class='span-value-copy' contenteditable='true' style='background:#"+hex+"'>#"+hex+"</span>"); 
 

 
function copyTextToClipboard(text) { 
 
    var textArea = document.createElement("textarea"); 
 
    textArea.value = text; 
 
    document.body.appendChild(textArea); 
 
    textArea.select(); 
 
    document.execCommand('copy'); 
 
    document.body.removeChild(textArea); 
 
} 
 

 
var copyBobBtn = document.querySelector('.span-value-copy'); 
 

 
copyBobBtn.addEventListener('click', function(event) { 
 
    copyTextToClipboard(hex); 
 
}); 
 
    
 
    }); 
 

 
\t function rgbToHex(R, G, B) { 
 
\t return toHex(R) + toHex(G) + toHex(B); 
 
\t } 
 

 
\t function toHex(n) { 
 
\t n = parseInt(n, 10); 
 
\t if (isNaN(n)) return "00"; 
 
\t n = Math.max(0, Math.min(n, 255)); 
 
\t return "ABCDEF".charAt((n - n % 16)/16) + "ABCDEF".charAt(n % 16); 
 
\t } 
 

 

 

 
    });
/* latin-ext */ 
 
@font-face { 
 
    font-family: 'Lato'; 
 
    font-style: normal; 
 
    font-weight: 300; 
 
    src: local('Lato Light'), local('Lato-Light'), url(http://fonts.gstatic.com/s/lato/v11/dPJ5r9gl3kK6ijoeP1IRsvY6323mHUZFJMgTvxaG2iE.woff2) format('woff2'); 
 
    unicode-range: U+0100-024F, U+1E00-1EFF, U+20A0-20AB, U+20AD-20CF, U+2C60-2C7F, U+A720-A7FF; 
 
} 
 
/* latin */ 
 
@font-face { 
 
    font-family: 'Lato'; 
 
    font-style: normal; 
 
    font-weight: 300; 
 
    src: local('Lato Light'), local('Lato-Light'), url(http://fonts.gstatic.com/s/lato/v11/EsvMC5un3kjyUhB9ZEPPwg.woff2) format('woff2'); 
 
    unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215, U+E0FF, U+EFFD, U+F000; 
 
} 
 

 
body { 
 
    background: hsl(184,65%,49%); 
 
    font-family: 'Lato'; 
 
    color: #000; 
 
    font: 15px/1.4em; 
 
} 
 

 
canvas{ 
 
    background: hsl(184,65%,49%); 
 
    
 
} 
 

 
#picked span{ 
 
    display:inline-block; 
 
    width:50px; 
 
    height:50px; 
 
    margin:3px; 
 
    text-align:center; 
 
    text-shadow:1px 1px 1px #000; 
 
    font:8px/50px Arial; 
 
    color:#fff; 
 
} 
 

 
.js .inputfile { 
 
    width: 0.1px; 
 
    height: 0.1px; 
 
    opacity: 0; 
 
    overflow: hidden; 
 
    position: absolute; 
 
    z-index: -1; 
 
} 
 

 
.inputfile + label { 
 
    max-width: 80%; 
 
    font-weight: 700; 
 
    text-overflow: ellipsis; 
 
    white-space: nowrap; 
 
    cursor: pointer; 
 
    display: inline-block; 
 
    overflow: hidden; 
 
    padding: 0.625rem 1.25rem; 
 
} 
 

 
.no-js .inputfile + label { 
 
    display: none; 
 
} 
 

 
.inputfile:focus + label, 
 
.inputfile.has-focus + label { 
 
    outline: 1px dotted #000; 
 
    outline: -webkit-focus-ring-color auto 5px; 
 
} 
 

 

 
.inputfile + label svg { 
 
    width: 1em; 
 
    height: 1em; 
 
    vertical-align: middle; 
 
    fill: currentColor; 
 
    margin-top: -0.25em; 
 
    margin-right: 0.25em; 
 
} 
 

 

 
.inputfile-2 + label { 
 
    width: 90%; 
 
    max-width: 220px; 
 
    background: #fff; 
 
    color: #333; 
 
    border: none; 
 
    font-family: Lato; 
 
\t text-align: center; 
 
    font-size: 1vw; 
 
    padding: 25px 0 25px 0; 
 
    display: inline-block; 
 
    letter-spacing: 1px; 
 
    font-weight: 700; 
 
    outline: none; 
 
    position: relative; 
 
    -webkit-transition: all 0.3s; 
 
    -moz-transition: all 0.3s; 
 
    transition: all 0.3s; 
 
    border: 3px solid #333; 
 
} 
 

 

 

 
/* COLUMN SETUP */ 
 
.col2 { 
 
\t display: block; 
 
\t float:left; 
 
\t margin: 0; 
 
} 
 
.col2:first-child { margin-left: 0; } 
 

 
/* GROUPING */ 
 
.group2:before, 
 
.group2:after { content:""; display:table; } 
 
.group2:after { clear:both;} 
 
.group2 { zoom:1; /* For IE 6/7 */ } 
 

 
/* GRID OF THREE */ 
 
.span_3_of_3 { width: 100%; } 
 
.span_2_of_3 { width: 66.66%; } 
 
.span_1_of_3 { width: 33.33%; } 
 

 
/* GO FULL WIDTH BELOW 480 PIXELS */ 
 
@media only screen and (max-width: 480px) { 
 
\t .col2 { margin: 0 } 
 
\t .span_3_of_3, .span_2_of_3, .span_1_of_3 { width: 100%; } 
 
} 
 

 

 
/* COLUMN SETUP */ 
 
.col { 
 
\t display: block; 
 
\t float:left; 
 
\t margin: 1% 0 1% 2%; 
 
} 
 
.col:first-child { margin-left: 0; } 
 

 
/* GROUPING */ 
 
.group:before, 
 
.group:after { content:""; display:table; } 
 
.group:after { clear:both;} 
 
.group { zoom:1; /* For IE 6/7 */ } 
 
/* GRID OF FOUR */ 
 
.span_4_of_4 { 
 
\t width: 100%; 
 
} 
 
.span_3_of_4 { 
 
\t width: 74.5%; 
 
} 
 
.span_2_of_4 { 
 
\t width: 49%; 
 
} 
 
.span_1_of_4 { 
 
\t width: 23.5%; 
 
} 
 

 
/* GO FULL WIDTH BELOW 480 PIXELS */ 
 
@media only screen and (max-width: 480px) { 
 
\t .col { margin: 1% 0 1% 0%; } 
 
\t .span_1_of_4, .span_2_of_4, .span_3_of_4, .span_4_of_4 { width: 100%; } 
 
} 
 

 

 
/* SECTIONS */ 
 
.section { 
 
\t margin-left:5%; 
 
\t margin-right:5%; 
 
\t clear: both; 
 
} 
 

 
/* COLUMN SETUP */ 
 
.col { 
 
\t display: block; 
 
\t float:left; 
 
\t margin: 1% 0 1% 2%; 
 
} 
 
.col:first-child { margin-left: 0; } 
 

 
/* GROUPING */ 
 
.group:before, 
 
.group:after { content:""; display:table; } 
 
.group:after { clear:both;} 
 
.group { zoom:1; /* For IE 6/7 */ } 
 
/* GRID OF FOUR */ 
 
.span_4_of_4 { 
 
\t width: 100%; 
 
} 
 
.span_3_of_4 { 
 
\t width: 74.5%; 
 
} 
 
.span_2_of_4 { 
 
\t width: 49%; 
 
} 
 
.span_1_of_4 { 
 
\t width: 23.5%; 
 
} 
 

 
/* GO FULL WIDTH BELOW 480 PIXELS */ 
 
@media only screen and (max-width: 480px) { 
 
\t .col { margin: 1% 0 1% 0%; } 
 
\t .span_1_of_4, .span_2_of_4, .span_3_of_4, .span_4_of_4 { width: 100%; } 
 
}
<html lang="en" class="no-js"> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
\t <div id="picked"></div> 
 
\t <div class="section group"> 
 
\t \t <div class="col span_1_of_4"> 
 
\t \t  <input type="file" name="file_upload[]" id="file_upload" class="inputfile inputfile-2" data-multiple-caption="{count} files selected" multiple /><label id="fileLabel" for="file_upload"><svg xmlns="http://www.w3.org/2000/svg" width="20" height="17" viewBox="0 0 20 17"><path d="M10 0l-5.2 4.9h3.3v5.1h3.8v-5.1h3.3l-5.2-4.9zm9.3 11.5l-3.2-2.1h-2l3.4 2.6h-3.5c-.1 0-.2.1-.2.1l-.8 2.3h-6l-.8-2.2c-.1-.1-.1-.2-.2-.2h-3.6l3.4-2.6h-2l-3.2 2.1c-.4.3-.7 1-.6 1.5l.6 3.1c.1.5.7.9 1.2.9h16.3c.6 0 1.1-.4 1.3-.9l.6-3.1c.1-.5-.2-1.2-.7-1.5z"/></svg><span>CHOOSE A FILE</span></label> 
 
\t \t </div> 
 
\t \t <div class="col span_3_of_4"> 
 
\t \t  <canvas width="600" height="300" id="canvas_picker"></canvas> 
 
\t \t </div> 
 
\t </div> 
 
    </html>

ユーザーHarshilパテルは動作しませんでした答えを与えました。コードをちょっと微調整して動作させました。 P正しいJSのクエリは、ここに示されています::だから、クレジットはあまりにもこのユーザに配布されている

まだ
$(function() { 
//Script that hides the input box (label is a substitute) 
(function(e,t,n){var r=e.querySelectorAll("html")[0];r.className=r.className.replace(/(^|\s)no-js(\s|$)/,"$1js$2")})(document,window,0); 

//Hex picker function 
var $picked = $("#picked"); // Just to preview picked colors 
    var canvas = $('#canvas_picker')[0]; 
    var context = canvas.getContext('2d'); 


    $("#file_upload").change(function (e) { 
     var F = this.files[0]; 
     var reader = new FileReader(); 
     reader.onload = imageIsLoaded; 
     reader.readAsDataURL(F); 
    }); 

$('#fileLabel').bind({ 
    dragover: function(e){ 
     e.preventDefault(); 
     e.stopPropagation(); 
    }, 
    dragleave: function(e){ 
     e.preventDefault(); 
     e.stopPropagation(); 
    }, 
    drop: function(e){ 
     e.preventDefault(); 
     e.stopPropagation(); 
     var F = e.originalEvent.dataTransfer.files[0]; 
     var reader = new FileReader(); 
     reader.onload = imageIsLoaded; 
     reader.readAsDataURL(F); 
    } 
}); 

    function imageIsLoaded(e) { 
     var img = new Image(); 
     img.onload = function(){ 
     canvas.width = this.width; 
     canvas.height = this.height; 
     context.drawImage(this, 0, 0); 
     }; 
     img.src = e.target.result; 
    } 

    function rgbToHex(R, G, B) { 
     return toHex(R) + toHex(G) + toHex(B); 
    } 

    function toHex(n) { 
     n = parseInt(n, 10); 
     if (isNaN(n)) return "00"; 
     n = Math.max(0, Math.min(n, 255)); 
     return "ABCDEF".charAt((n - n % 16)/16) + "ABCDEF".charAt(n % 16); 
    } 

$('#canvas_picker').click(function(event){ 
        var x = event.pageX - $(this).offset().left; 
        var y = event.pageY - $(this).offset().top; 
        var img_data = context.getImageData(x,y , 1, 1).data; 
        var R = img_data[0]; 
        var G = img_data[1]; 
        var B = img_data[2]; 
        var rgb = R + ',' + G + ',' + B ; 
        var hex = rgbToHex(R,G,B); 
        $('#rgb input').val(rgb); 
        $('#hex input').val('#' + hex); 
        $picked.append("<span data-value='#"+hex+"' class='span-value-copy-"+hex+"' contenteditable='true' style='background:#"+hex+"'>#"+hex+"</span>"); 

        function copyTextToClipboard(text) { 
         var textArea = document.createElement("textarea"); 
         textArea.value = text; 
         document.body.appendChild(textArea); 
         textArea.select(); 
         document.execCommand('copy'); 
         document.body.removeChild(textArea); 
        } 

        var copyBobBtn = document.querySelector(".span-value-copy-"+hex); 

        copyBobBtn.addEventListener('click', function() { 
         var currentHex = $(this).data('value'); 
         copyTextToClipboard(currentHex); 
        }); 

       }); 


    }); 

答えて

1
$('#canvas_picker').click(function(event){ 
        var x = event.pageX - $(this).offset().left; 
        var y = event.pageY - $(this).offset().top; 
        var img_data = context.getImageData(x,y , 1, 1).data; 
        var R = img_data[0]; 
        var G = img_data[1]; 
        var B = img_data[2]; 
        var rgb = R + ',' + G + ',' + B ; 
        var hex = rgbToHex(R,G,B); 
        $('#rgb input').val(rgb); 
        $('#hex input').val('#' + hex); 
        $picked.append("<span data-value='#"+hex+"' class='span-value-copy' contenteditable='true' style='background:#"+hex+"'>#"+hex+"</span>"); 

        function copyTextToClipboard(text) { 
         var textArea = document.createElement("textarea"); 
         textArea.value = text; 
         document.body.appendChild(textArea); 
         textArea.select(); 
         document.execCommand('copy'); 
         document.body.removeChild(textArea); 
        } 

        var copyBobBtn = document.querySelector('.span-value-copy'); 

        copyBobBtn.addEventListener('click', function() { 
         var currentHex = $(this).data('value'); 
         copyTextToClipboard(currentHex); 
        }); 

       }); 
+0

それはコピーのみ最初のボックス。 –

+0

私は答えを更新しました。これはうまくいかなかった。私はクラス名に変数を追加しています。私は正しい答えを受け入れることができますが、私はちょっと編集する必要があると思います(私のバージョンをアップデートからコピーしてください)。それは貢献物です:P –

関連する問題