[スクリプトの概要]
描画条件を選択していくことで、オリジナルエンブレムを作成するスクリプト

  1. //=============================================================================
  2. //
  3. // GkEmblemMaker.js JAVAScript Emblem Maker エンブレムメーカー
  4. //            Programmed by G.K.2018
  5. //
  6. //=============================================================================
  7. //変数の宣言===================================================================
  8. var canvas; //キャンバスオブジェクト
  9. var ctx; //描画コンテキスト
  10. var winWidth = 500; //CANVASの横幅
  11. var winHeight = 600; //CANVASの高さ
  12. var selectStep = 0; //選択項目のステップ数
  13. var selectValue = new Array(); //選択値の配列
  14. var mesStr = new Array(); //表示メッセージ
  15. var cx = 0; //クリック(タッチ)位置X
  16. var cy = 0; //クリック(タッチ)位置Y
  17. var bton = 0; //タッチしたボタンの番号
  18. //関数の宣言========================================================================
  19. //クリック(タップ)時のイベント----------------------
  20. function onDown(e) {
  21. var rect = e.target.getBoundingClientRect();
  22. cx = parseInt(e.clientX - rect.left);
  23. cy = parseInt(e.clientY - rect.top);
  24. if(bton == 0) {
  25. checkButtonOn();
  26. }
  27. draw();
  28. }
  29. //タップ時のイベント----------------------
  30. function onDown2(e) {
  31. e.preventDefault();
  32. var rect = e.target.getBoundingClientRect();
  33. var touchObject = e.changedTouches[0];
  34. cx = parseInt(touchObject.clientX - rect.left);
  35. cy = parseInt(touchObject.clientY - rect.top);
  36. if(bton == 0) {
  37. checkButtonOn();
  38. }
  39. draw();
  40. }
  41. //初期化処理-------------------------------------------
  42. function init() {
  43. //描画コンテキストの取得
  44. canvas = document.getElementById("mainCanvas");
  45. if ( ! canvas || ! canvas.getContext ) { return false; }
  46. ctx = canvas.getContext('2d');
  47. //イベントの追加
  48. canvas.addEventListener('mousedown', onDown, false);
  49. canvas.addEventListener('touchstart', onDown2, false);
  50. selectStep = 0;
  51. //選択値の初期化
  52. for(var i=0; i<11; i++) {
  53. selectValue[i] = -1;
  54. }
  55. }
  56. //背景を描画------------------------------------------
  57. function draw() {
  58. ctx.clearRect(0, 0, 500, 600); //画面のクリア
  59. draw_Emblem();
  60. draw_Menu();
  61. //メッセージの表示
  62. set_MessageStr();
  63. ctx.fillStyle = "rgb(0, 0, 0)";
  64. ctx.font = "20px 'MS Pゴシック'"; //表示フォントの設定
  65. ctx.fillText(mesStr[0], 10, 20);
  66. ctx.fillText(mesStr[1], 30, 50);
  67. ctx.fillText(mesStr[2], 30, 75);
  68. bton = 0;
  69. }
  70. //最初に実行------------------------------------------
  71. function main(){
  72. init();
  73. draw();
  74. }
  75. //表示メッセージをセットする--------------------------
  76. function set_MessageStr() {
  77. mesStr[0] = "";
  78. mesStr[1] = "";
  79. mesStr[2] = "";
  80. switch(selectStep) {
  81. case 0:
  82. mesStr[0] = "[はじめましょう]";
  83. mesStr[1] = "画面をタップすると選択項目が出てくるので、";
  84. mesStr[2] = "お好きな色や形を選んでください。";
  85. break;
  86. case 1:
  87. mesStr[0] = "[ステップ 01/10]";
  88. mesStr[1] = "背景色を選んでください。(左上4分の1)";
  89. break;
  90. case 2:
  91. mesStr[0] = "[ステップ 02/10]";
  92. mesStr[1] = "背景色を選んでください。(右上4分の1)";
  93. break;
  94. case 3:
  95. mesStr[0] = "[ステップ 03/10]";
  96. mesStr[1] = "背景色を選んでください。(左下4分の1)";
  97. break;
  98. case 4:
  99. mesStr[0] = "[ステップ 04/10]";
  100. mesStr[1] = "背景色を選んでください。(右下4分の1)";
  101. break;
  102. case 5:
  103. mesStr[0] = "[ステップ 05/10]";
  104. mesStr[1] = "中央円の大きさを選んでください。";
  105. break;
  106. case 6:
  107. mesStr[0] = "[ステップ 06/10]";
  108. mesStr[1] = "中央円の色を選んでください。";
  109. break;
  110. case 7:
  111. mesStr[0] = "[ステップ 07/10]";
  112. mesStr[1] = "四角形の位置を選んでください。";
  113. break;
  114. case 8:
  115. mesStr[0] = "[ステップ 08/10]";
  116. mesStr[1] = "四角形の色を選んでください。";
  117. break;
  118. case 9:
  119. mesStr[0] = "[ステップ 09/10]";
  120. mesStr[1] = "小円の位置を選んでください。";
  121. break;
  122. case 10:
  123. mesStr[0] = "[ステップ 10/10]";
  124. mesStr[1] = "小円の色を選んでください。";
  125. break;
  126. case 11:
  127. mesStr[1] = "完成!!!!";
  128. break;
  129. }
  130. //mesStr[2] = " " + cx + "," + cy + "," + bton; //debug
  131. }
  132. //エンブレムを条件に従って描画する--------------------
  133. function draw_Emblem() {
  134. var xpos = 0;
  135. var ypos = 0;
  136. ctx.globalAlpha = 1;
  137. //[STEP 01] 背景-左上
  138. if(selectValue[1] > -1) {
  139. set_Color(selectValue[1]);
  140. ctx.fillRect(0, 100, 250, 250);
  141. }
  142. //[STEP 02] 背景-右上
  143. if(selectValue[2] > -1) {
  144. set_Color(selectValue[2]);
  145. ctx.fillRect(250, 100, 250, 250);
  146. }
  147. //[STEP 03] 背景-左下
  148. if(selectValue[3] > -1) {
  149. set_Color(selectValue[3]);
  150. ctx.fillRect(0, 350, 250, 250);
  151. }
  152. //[STEP 04] 背景-右下
  153. if(selectValue[4] > -1) {
  154. set_Color(selectValue[4]);
  155. ctx.fillRect(250, 350, 250, 250);
  156. }
  157. //[STEP 05、06] 中央円
  158. if(selectValue[5] > -1) {
  159. var radius = 200;
  160. if(selectValue[5] == 0 || selectValue[5] == 1 || selectValue[5] == 2) { radius = 270; }
  161. if(selectValue[5] == 3 || selectValue[5] == 4 || selectValue[5] == 5) { radius = 180; }
  162. if(selectValue[5] == 6 || selectValue[5] == 7 || selectValue[5] == 8) { radius = 90; }
  163. ctx.beginPath();
  164. ctx.arc(250, 350, radius, 0, Math.PI*2, true);
  165. if(selectValue[6] == -1) {
  166. //線のみ
  167. ctx.stroke();
  168. }
  169. else
  170. {
  171. //塗りつぶしあり
  172. set_Color(selectValue[6]);
  173. ctx.fill() ;
  174. }
  175. }
  176. //[STEP 07、08] 四角形
  177. if(selectValue[7] > -1) {
  178. if(selectValue[7] == 0 || selectValue[7] == 3 || selectValue[7] == 6) { xpos = 0; }
  179. if(selectValue[7] == 1 || selectValue[7] == 4 || selectValue[7] == 7) { xpos = 166; }
  180. if(selectValue[7] == 2 || selectValue[7] == 5 || selectValue[7] == 8) { xpos = 332; }
  181. if(selectValue[8] == -1) {
  182. //線のみ
  183. ctx.strokeStyle = "rgb(0, 0, 0)";
  184. ctx.strokeRect(xpos, 100, 166, 500);
  185. }
  186. else
  187. {
  188. //塗りつぶしあり
  189. set_Color(selectValue[8]);
  190. ctx.fillRect(xpos, 100, 166, 500);
  191. }
  192. }
  193. //[STEP 09、10] 小丸
  194. if(selectValue[9] > -1) {
  195. switch(selectValue[9])
  196. {
  197. case 0: xpos = 83; ypos = 83; break;
  198. case 1: xpos = 250; ypos = 83; break;
  199. case 2: xpos = 417; ypos = 83; break;
  200. case 3: xpos = 83; ypos = 250; break;
  201. case 4: xpos = 250; ypos = 250; break;
  202. case 5: xpos = 417; ypos = 250; break;
  203. case 6: xpos = 83; ypos = 417; break;
  204. case 7: xpos = 250; ypos = 417; break;
  205. case 8: xpos = 417; ypos = 417; break;
  206. }
  207. ctx.beginPath();
  208. ctx.arc(xpos, ypos+100, 80, 0, Math.PI*2, true);
  209. if(selectValue[10] == -1) {
  210. //線のみ
  211. ctx.stroke();
  212. }
  213. else
  214. {
  215. //塗りつぶしあり
  216. set_Color(selectValue[10]);
  217. ctx.fill() ;
  218. }
  219. }
  220. //外枠の描画
  221. set_Color(0);
  222. ctx.fillRect(0, 0, 500, 100);
  223. ctx.globalAlpha = 1.0;
  224. ctx.strokeStyle = "rgb(0, 0, 0)";
  225. ctx.strokeRect(0, 100, 500, 500);
  226. }
  227. //メニューを表示する---------------------------------
  228. function draw_Menu() {
  229. if(selectStep > 10) {
  230. return;
  231. }
  232. ctx.globalAlpha = 0.8;
  233. ctx.fillStyle = "rgb(255, 255, 255)";
  234. ctx.fillRect(100, 200, 300, 300);
  235. ctx.strokeStyle = "rgb(0, 0, 0)";
  236. ctx.strokeRect(100, 200, 300, 300); //外枠の描画
  237. switch(selectStep) {
  238. case 0:
  239. ctx.fillStyle = "rgb(0, 0, 0)";
  240. ctx.font = "22px 'MS Pゴシック'"; //表示フォントの設定
  241. ctx.fillText("タッチしてください。", 170, 350);
  242. break;
  243. //色メニュー
  244. case 1:
  245. case 2:
  246. case 3:
  247. case 4:
  248. case 6:
  249. case 8:
  250. case 10:
  251. set_Color(0);
  252. ctx.fillRect(100, 200, 100, 100);
  253. set_Color(1);
  254. ctx.fillRect(200, 200, 100, 100);
  255. set_Color(2);
  256. ctx.fillRect(300, 200, 100, 100);
  257. set_Color(3);
  258. ctx.fillRect(100, 300, 100, 100);
  259. set_Color(4);
  260. ctx.fillRect(200, 300, 100, 100);
  261. set_Color(5);
  262. ctx.fillRect(300, 300, 100, 100);
  263. set_Color(6);
  264. ctx.fillRect(100, 400, 100, 100);
  265. set_Color(7);
  266. ctx.fillRect(200, 400, 100, 100);
  267. set_Color(8);
  268. ctx.fillRect(300, 400, 100, 100);
  269. break;
  270. //大きさメニュー大中小
  271. case 5:
  272. ctx.strokeRect(100, 200, 300, 100);
  273. ctx.strokeRect(100, 300, 300, 100);
  274. ctx.strokeRect(100, 400, 300, 100);
  275. ctx.fillStyle = "rgb(0, 0, 0)";
  276. ctx.font = "36px 'MS Pゴシック'"; //表示フォントの設定
  277. ctx.fillText("大", 232, 232+25);
  278. ctx.fillText("中", 232, 332+25);
  279. ctx.fillText("小", 232, 432+25);
  280. break;
  281. //位置メニュー左中右
  282. case 7:
  283. ctx.strokeRect(100, 200, 100, 300);
  284. ctx.strokeRect(200, 200, 100, 300);
  285. ctx.strokeRect(300, 200, 100, 300);
  286. ctx.fillStyle = "rgb(0, 0, 0)";
  287. ctx.font = "36px 'MS Pゴシック'"; //表示フォントの設定
  288. ctx.fillText("左", 132, 332+25);
  289. ctx.fillText("中", 232, 332+25);
  290. ctx.fillText("右", 332, 332+25);
  291. break;
  292. //位置メニュー9か所
  293. case 9:
  294. for (var i=0; i<3; i++) {
  295. for (var j=0; j<3; j++) {
  296. ctx.strokeRect(100 + j*100, 200 + i*100, 100, 100);
  297. }
  298. }
  299. ctx.fillStyle = "rgb(0, 0, 0)";
  300. ctx.font = "18px 'MS Pゴシック'"; //表示フォントの設定
  301. ctx.fillText("左上", 132, 232+25);
  302. ctx.fillText(" 上 ", 232, 232+25);
  303. ctx.fillText("右上", 332, 232+25);
  304. ctx.fillText(" 左 ", 132, 332+25);
  305. ctx.fillText("中央", 232, 332+25);
  306. ctx.fillText(" 右 ", 332, 332+25);
  307. ctx.fillText("左下", 132, 432+25);
  308. ctx.fillText(" 下 ", 232, 432+25);
  309. ctx.fillText("右下", 332, 432+25);
  310. break;
  311. }
  312. ctx.fillStyle = "rgb(255, 255, 255)";
  313. ctx.globalAlpha = 1.0;
  314. }
  315. //色をセットする--------------------------------------
  316. function set_Color(num) {
  317. switch(num) {
  318. case 0:
  319. ctx.fillStyle = "rgb(255, 255, 255)";
  320. break;
  321. case 1:
  322. ctx.fillStyle = "rgb(128, 128, 128)";
  323. break;
  324. case 2:
  325. ctx.fillStyle = "rgb(0, 0, 0)";
  326. break;
  327. case 3:
  328. ctx.fillStyle = "rgb(255, 0, 0)";
  329. break;
  330. case 4:
  331. ctx.fillStyle = "rgb(0, 255, 0)";
  332. break;
  333. case 5:
  334. ctx.fillStyle = "rgb(0, 0, 255)";
  335. break;
  336. case 6:
  337. ctx.fillStyle = "rgb(255, 255, 0)";
  338. break;
  339. case 7:
  340. ctx.fillStyle = "rgb(0, 255, 255)";
  341. break;
  342. case 8:
  343. ctx.fillStyle = "rgb(255, 0, 255)";
  344. break;
  345. }
  346. }
  347. //メニューを押しているかを調べる-----------------------
  348. function checkButtonOn() {
  349. bton = 0;
  350. //メニュー内のタッチチェック
  351. if(cx > 100 && cx < 400 && cy > 200 && cy < 500) {
  352. bton = parseInt((cx - 100) / 100) + parseInt((cy - 200) / 100) * 3;
  353. selectValue[selectStep] = bton;
  354. //選択ステップを進める
  355. if(selectStep < 11) {
  356. selectStep ++;
  357. }
  358. }
  359. }
  360. //ウィンドウを閉じる----------------------------------
  361. function MM_closeFloatWindow(theURL) {
  362. window.close(this);
  363. }
  364.  

[ファイルのダウンロード]
emblemmaker.zip

サンプルを表示
ウィンドウを閉じる
トップに戻る