The code should be displayed using a picture loop and by clicking the picture opens in a modal window in which you can click on any place in the picture to recognize its RGB and HEX. The problem is that the picture is not transferred to the modal window. For some reason, the variable from the loop is not passed to the variable in javascript.
<?php $dir = 'img/'; // ΠΠ°ΠΏΠΊΠ° Ρ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡΠΌΠΈ $cols = 3; // ΠΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ ΡΡΠΎΠ»Π±ΡΠΎΠ² Π² Π±ΡΠ΄ΡΡΠ΅ΠΉ ΡΠ°Π±Π»ΠΈΡΠ΅ Ρ ΠΊΠ°ΡΡΠΈΠ½ΠΊΠ°ΠΌΠΈ $files = scandir($dir); // ΠΠ΅ΡΡΠΌ Π²ΡΡ ΡΠΎΠ΄Π΅ΡΠΆΠΈΠΌΠΎΠ΅ Π΄ΠΈΡΠ΅ΠΊΡΠΎΡΠΈΠΈ //echo "<div class='row'>"; // $k = 0; // for ($i = 0; $i < count($files); $i++) { // ΠΠ΅ΡΠ΅Π±ΠΈΡΠ°Π΅ΠΌ Π²ΡΠ΅ ΡΠ°ΠΉΠ»Ρ if (($files[$i] != ".") && ($files[$i] != "..")) { // Π’Π΅ΠΊΡΡΠΈΠΉ ΠΊΠ°ΡΠ°Π»ΠΎΠ³ ΠΈ ΡΠΎΠ΄ΠΈΡΠ΅Π»ΡΡΠΊΠΈΠΉ ΠΏΡΠΎΠΏΡΡΠΊΠ°Π΅ΠΌ if ($k % $cols == 0) ; // ΠΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ Π½ΠΎΠ²ΡΡ ΡΡΡΠΎΠΊΡ echo "<div class='col-xs-6 col-sm-4 col-md-3 col-lg-3'>"; echo "<div class='thumbnail'>"; // $path = $dir.$files[$i]; // ΠΠΎΠ»ΡΡΠ°Π΅ΠΌ ΠΏΡΡΡ ΠΊ ΠΊΠ°ΡΡΠΈΠ½ΠΊΠ΅ echo "<a data-toggle='modal' data-target='#myModal$i'>"; // ΠΠ΅Π»Π°Π΅ΠΌ ΡΡΡΠ»ΠΊΡ Π½Π° ΠΊΠ°ΡΡΠΈΠ½ΠΊΡ echo "<img src='$path' alt='' width='300' height='240' />"; // ΠΡΠ²ΠΎΠ΄ ΠΏΡΠ΅Π²ΡΡ ΠΊΠ°ΡΡΠΈΠ½ΠΊΠΈ echo "</a>"; // ΠΠ°ΠΊΡΡΠ²Π°Π΅ΠΌ ΡΡΡΠ»ΠΊΡ echo "</div>"; echo "</div>"; echo " <div class='modal fade' id='myModal$i' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'> <div class='modal-dialog modal-lg'> <div class='modal-content'> <div class='modal-header'> <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>×</button> <h4 class='modal-title' id='myModalLabel'>ΠΠ°Π·Π²Π°Π½ΠΈΠ΅ ΠΌΠΎΠ΄Π°Π»ΠΈ</h4> </div> <div class='modal-body'> <canvas width='600' height='440' id='canvas_picker'></canvas> <div id='hex'>HEX: <input type='text'></input></div> <div id='rgb'>RGB: <input type='text'></input></div> </div> <div class='modal-footer'> <button type='button' class='btn btn-default' data-dismiss='modal'>ΠΠ°ΠΊΡΡΡΡ</button> <button type='button' class='btn btn-primary'>Π‘ΠΎΡ
ΡΠ°Π½ΠΈΡΡ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΡ</button> </div> </div> </div> </div>"; echo "<script type='text/javascript'> var canvas = document.getElementById('canvas_picker').getContext('2d'); // create an image object and get it's source var img = new Image(); img.src = '".$path."'; // image credit: http://pixabay.com/en/fishing-boat-denmark-beach-sea-49523/ // copy the image to the canvas $(img).load(function(){ canvas.drawImage(img,0,0); }); // http://www.javascripter.net/faq/rgbtohex.htm 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 '0123456789ABCDEF'.charAt((nn%16)/16) + '0123456789ABCDEF'.charAt(n%16); } $('#canvas_picker').click(function(event){ // getting user coordinates var x = event.pageX - this.offsetLeft; var y = event.pageY - this.offsetTop; // getting image data and RGB values var img_data = canvas.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; // convert RGB to HEX var hex = rgbToHex(R,G,B); // making the color the value of the input $('#rgb input').val(rgb); $('#hex input').val('#' + hex); }); </script>"; // echo "</div>"; // ΠΠ°ΠΊΡΡΠ²Π°Π΅ΠΌ ΡΡΠΎΠ»Π±Π΅Ρ /* ΠΠ°ΠΊΡΡΠ²Π°Π΅ΠΌ ΡΡΡΠΎΠΊΡ, Π΅ΡΠ»ΠΈ Π½Π΅ΠΎΠ±Ρ
ΠΎΠ΄ΠΈΠΌΠΎΠ΅ ΠΊΠΎΠ»ΠΈΡΠ΅ΡΡΠ²ΠΎ Π±ΡΠ»ΠΎ Π²ΡΠ²Π΅Π΄Π΅Π½ΠΎ, Π»ΠΈΠ±ΠΎ Π΄Π°Π½Π½Π°Ρ ΠΈΡΠ΅ΡΠ°ΡΠΈΡ ΠΏΠΎΡΠ»Π΅Π΄Π½ΡΡ */ if ((($k + 1) % $cols == 0) || (($i + 1) == count($files))); $k++; // Π£Π²Π΅Π»ΠΈΡΠΈΠ²Π°Π΅ΠΌ Π²ΡΠΏΠΎΠΌΠΎΠ³Π°ΡΠ΅Π»ΡΠ½ΡΠΉ ΡΡΡΡΡΠΈΠΊ } } ?> The console says that the error in
$(img).load(function(){ canvas.drawImage(img,0,0); }); Only xs how to remove it. Help me please.
In html everything seems to be fine and the php variable passes the value, but javascript does not want to work with it. Here is the page code:
<div class="container"> <div class="panel panel-default"> <div class="panel-body"> <div class="row"> <div class='col-xs-6 col-sm-4 col-md-3 col-lg-3'><div class='thumbnail'><a data-toggle='modal' data-target='#myModal2'><img src='img/006.jpg' alt='' width='300' height='240' /></a></div></div> <div class='modal fade' id='myModal2' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'> <div class='modal-dialog modal-lg'> <div class='modal-content'> <div class='modal-header'> <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>×</button> <h4 class='modal-title' id='myModalLabel'>ΠΠ°Π·Π²Π°Π½ΠΈΠ΅ ΠΌΠΎΠ΄Π°Π»ΠΈ</h4> </div> <div class='modal-body'> <canvas width='600' height='440' id='canvas_picker'></canvas> <div id='hex'>HEX: <input type='text'></input></div> <div id='rgb'>RGB: <input type='text'></input></div> </div> <div class='modal-footer'> <button type='button' class='btn btn-default' data-dismiss='modal'>ΠΠ°ΠΊΡΡΡΡ</button> <button type='button' class='btn btn-primary'>Π‘ΠΎΡ
ΡΠ°Π½ΠΈΡΡ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΡ</button> </div> </div> </div> </div><script type='text/javascript'> var canvas = document.getElementById('canvas_picker').getContext('2d'); // create an image object and get it's source var img = new Image(); img.src = 'img/006.jpg'; // image credit: http://pixabay.com/en/fishing-boat-denmark-beach-sea-49523/ // copy the image to the canvas $(img).load(function(){ canvas.drawImage(img,0,0); }); // http://www.javascripter.net/faq/rgbtohex.htm 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 '0123456789ABCDEF'.charAt((nn%16)/16) + '0123456789ABCDEF'.charAt(n%16); } $('#canvas_picker').click(function(event){ // getting user coordinates var x = event.pageX - this.offsetLeft; var y = event.pageY - this.offsetTop; // getting image data and RGB values var img_data = canvas.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; // convert RGB to HEX var hex = rgbToHex(R,G,B); // making the color the value of the input $('#rgb input').val(rgb); $('#hex input').val('#' + hex); }); </script><div class='col-xs-6 col-sm-4 col-md-3 col-lg-3'><div class='thumbnail'><a data-toggle='modal' data-target='#myModal3'><img src='img/007.jpg' alt='' width='300' height='240' /></a></div></div> <div class='modal fade' id='myModal3' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'> <div class='modal-dialog modal-lg'> <div class='modal-content'> <div class='modal-header'> <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>×</button> <h4 class='modal-title' id='myModalLabel'>ΠΠ°Π·Π²Π°Π½ΠΈΠ΅ ΠΌΠΎΠ΄Π°Π»ΠΈ</h4> </div> <div class='modal-body'> <canvas width='600' height='440' id='canvas_picker'></canvas> <div id='hex'>HEX: <input type='text'></input></div> <div id='rgb'>RGB: <input type='text'></input></div> </div> <div class='modal-footer'> <button type='button' class='btn btn-default' data-dismiss='modal'>ΠΠ°ΠΊΡΡΡΡ</button> <button type='button' class='btn btn-primary'>Π‘ΠΎΡ
ΡΠ°Π½ΠΈΡΡ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΡ</button> </div> </div> </div> </div><script type='text/javascript'> var canvas = document.getElementById('canvas_picker').getContext('2d'); // create an image object and get it's source var img = new Image(); img.src = 'img/007.jpg'; // image credit: http://pixabay.com/en/fishing-boat-denmark-beach-sea-49523/ // copy the image to the canvas $(img).load(function(){ canvas.drawImage(img,0,0); }); // http://www.javascripter.net/faq/rgbtohex.htm 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 '0123456789ABCDEF'.charAt((nn%16)/16) + '0123456789ABCDEF'.charAt(n%16); } $('#canvas_picker').click(function(event){ // getting user coordinates var x = event.pageX - this.offsetLeft; var y = event.pageY - this.offsetTop; // getting image data and RGB values var img_data = canvas.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; // convert RGB to HEX var hex = rgbToHex(R,G,B); // making the color the value of the input $('#rgb input').val(rgb); $('#hex input').val('#' + hex); }); </script><div class='col-xs-6 col-sm-4 col-md-3 col-lg-3'><div class='thumbnail'><a data-toggle='modal' data-target='#myModal4'><img src='img/008.jpg' alt='' width='300' height='240' /></a></div></div> <div class='modal fade' id='myModal4' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'> <div class='modal-dialog modal-lg'> <div class='modal-content'> <div class='modal-header'> <button type='button' class='close' data-dismiss='modal' aria-hidden='true'>×</button> <h4 class='modal-title' id='myModalLabel'>ΠΠ°Π·Π²Π°Π½ΠΈΠ΅ ΠΌΠΎΠ΄Π°Π»ΠΈ</h4> </div> <div class='modal-body'> <canvas width='600' height='440' id='canvas_picker'></canvas> <div id='hex'>HEX: <input type='text'></input></div> <div id='rgb'>RGB: <input type='text'></input></div> </div> <div class='modal-footer'> <button type='button' class='btn btn-default' data-dismiss='modal'>ΠΠ°ΠΊΡΡΡΡ</button> <button type='button' class='btn btn-primary'>Π‘ΠΎΡ
ΡΠ°Π½ΠΈΡΡ ΠΈΠ·ΠΌΠ΅Π½Π΅Π½ΠΈΡ</button> </div> </div> </div> </div><script type='text/javascript'> var canvas = document.getElementById('canvas_picker').getContext('2d'); // create an image object and get it's source var img = new Image(); img.src = 'img/008.jpg'; // image credit: http://pixabay.com/en/fishing-boat-denmark-beach-sea-49523/ // copy the image to the canvas $(img).load(function(){ canvas.drawImage(img,0,0); }); // http://www.javascripter.net/faq/rgbtohex.htm 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 '0123456789ABCDEF'.charAt((nn%16)/16) + '0123456789ABCDEF'.charAt(n%16); } $('#canvas_picker').click(function(event){ // getting user coordinates var x = event.pageX - this.offsetLeft; var y = event.pageY - this.offsetTop; // getting image data and RGB values var img_data = canvas.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; // convert RGB to HEX var hex = rgbToHex(R,G,B); // making the color the value of the input $('#rgb input').val(rgb); $('#hex input').val('#' + hex); });