前提・実現したいこと
Canvasに描画した矩形をglobalCompositeOperationであらかじめ用意したハート形の画像の形に切り抜いています。描画したハート形の色をテキストウインドウに入力した色(例「pink」)に変化させたいのですが、動作しません。どなたかご教示お願い申し上げます。
該当のソースコード
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <meta http-equiv="Content-Language" content="ja"> <title>test</title> <style> body {background:#eee;} canvas {background:#fff;} </style> </head> <body> <p><input type="text" id="rectcolor" value="#"> 入力した色に変換</p> <canvas id="canvas"></canvas> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> (function() { // 'use strict'; var stage = document.getElementById('canvas'); var ctx; var width = 300; var height = 300; ctx = stage.getContext('2d'); stage.width = width; stage.height = height; ctx.fillStyle = 'red'; ctx.fillRect(0, 0, 300, 300); ctx.globalCompositeOperation = "destination-in"; var img = new Image(); img.addEventListener("load", function() { }, false); img.onload = function(){ ctx.drawImage(img,0, 0, 300, 300); }; img.src = 'http://vps6-d.kuku.lu/files/20180214-1933_fea10d03f2098c4b45e5b4c3e1ee5974.png'; $('#rectcolor').change(function() { ctx.fillStyle = $(this).val(); ctx.fillRect(0, 0, 300, 300); }); })(); </script> </body> </html>試したこと
globalCompositeOperationを使用せずに矩形のままだと色が変わります。
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <meta http-equiv="Content-Language" content="ja"> <title>test</title> <style> body {background:#eee;} canvas {background:#fff;} </style> </head> <body> <p><input type="text" id="rectcolor" value="#"> 入力した色に変換</p> <canvas id="canvas"></canvas> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script type="text/javascript"> (function() { // 'use strict'; var stage = document.getElementById('canvas'); var ctx; var width = 300; var height = 300; ctx = stage.getContext('2d'); stage.width = width; stage.height = height; ctx.fillStyle = 'red'; ctx.fillRect(0, 0, 300, 300);$('#rectcolor').change(function() { ctx.fillStyle = $(this).val(); ctx.fillRect(0, 0, 300, 300); });
})();
</script>
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/14 12:20