国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > HTML > 正文

分享利用 HTML5 的 Canvas 制作人臉的實例代碼

2020-03-24 16:02:47
字體:
來源:轉載
供稿:網友
這里主要使用了 HTML5 的 Canvas 進行繪制。利用html5制作人臉的實例代碼。

先看我們要繪制的人臉效果圖:

用 HTML5 繪制人臉

這里主要使用了 HTML5 的 Canvas 進行繪制。

下面我們開始整個繪制過程:

1. HTML (index.html)

 !DOCTYPE html  html lang= en  head  meta charset= utf-8 /  title HTML5 Face Builder | Script Tutorials /title  link href= css/main.css rel= stylesheet type= text/css /  script src= http://code.jquery.com/jquery-latest.min.js /script  script type= text/javascript src= js/script.js /script  /head  body  header  h2 HTML5 image crop tool /h2  a href= http://www.script-tutorials.com/html5-face-builder/ >

2. js/script.js

// inner variablesvar canvas, ctx;var oHead, oEye, oNose, oMouth;var iSel = 0;// -------------------------------------------------------------// objects :function Head(x, y, x2, y2, w, h, image) { this.x = x; this.y = y; this.x2 = x2; this.y2 = y2; this.w = w; this.h = h; this.image = image; this.iSpr = 0;function Eye(x, y, x2, y2, w, h, image) { this.x = x; this.y = y; this.x2 = x2; this.y2 = y2; this.w = w; this.h = h; this.image = image; this.iSpr = 0;function Nose(x, y, x2, y2, w, h, image) { this.x = x; this.y = y; this.x2 = x2; this.y2 = y2; this.w = w; this.h = h; this.image = image; this.iSpr = 0;function Mouth(x, y, x2, y2, w, h, image) { this.x = x; this.y = y; this.x2 = x2; this.y2 = y2; this.w = w; this.h = h; this.image = image; this.iSpr = 0;// -------------------------------------------------------------// draw functions :function clear() { // clear canvas function ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);function drawScene() { // main drawScene function clear(); // clear canvas // draw head ctx.drawImage(oHead.image, oHead.x2 + oHead.iSpr*oHead.w, oHead.y2, oHead.w, oHead.h, oHead.x, oHead.y, oHead.w, oHead.h); // draw eyes ctx.drawImage(oEye.image, oEye.x2 + oEye.iSpr*oEye.w, oEye.y2, oEye.w, oEye.h, oEye.x, oEye.y, oEye.w, oEye.h); // draw nose ctx.drawImage(oNose.image, oNose.x2 + oNose.iSpr*oNose.w, oNose.y2, oNose.w, oNose.h, oNose.x, oNose.y, oNose.w, oNose.h); // draw mouth ctx.drawImage(oMouth.image, oMouth.x2 + oMouth.iSpr*oMouth.w, oMouth.y2, oMouth.w, oMouth.h, oMouth.x, oMouth.y, oMouth.w, oMouth.h); // draw controls ctx.textAlign = center  ctx.fillStyle = #000  ctx.font = 30px Verdana  if (iSel == 0) ctx.font = bold 30px Verdana  ctx.fillText( Head , 400, 80); ctx.font = 30px Verdana  if (iSel == 1) ctx.font = bold 30px Verdana  ctx.fillText( Eye , 400, 180); ctx.font = 30px Verdana  if (iSel == 2) ctx.font = bold 30px Verdana  ctx.fillText( Nose , 400, 280); ctx.font = 30px Verdana  if (iSel == 3) ctx.font = bold 30px Verdana  ctx.fillText( Mouth , 400, 380);// -------------------------------------------------------------// initialization$(function(){ canvas = document.getElementById( scene  ctx = canvas.getContext( 2d  // initialization of dragon var oHeadImage = new Image(); oHeadImage.src = images/image.png  oHeadImage.onload = function() {}; oHead = new Head(0, 0, 0, 755, 300, 405, oHeadImage); oEye = new Eye(40, 70, 0, 120, 235, 80, oHeadImage); oNose = new Nose(70, 120, 0, 276, 180, 140, oHeadImage); oMouth = new Mouth(60, 260, 0, 546, 170, 120, oHeadImage); $(window).keydown(function(event){ switch (event.keyCode) { case 38: // Up key iSel--; if (iSel 0) { iSel = 3; break; case 40: // Up key iSel++; if (iSel = 4) { iSel = 0; break; case 37: // Left key // update sprite positions if (iSel == 0) { oHead.iSpr--; if (oHead.iSpr 0) { oHead.iSpr = 3; if (iSel == 1) { oEye.iSpr--; if (oEye.iSpr 0) { oEye.iSpr = 4; if (iSel == 2) { oNose.iSpr--; if (oNose.iSpr 0) { oNose.iSpr = 4; if (iSel == 3) { oMouth.iSpr--; if (oMouth.iSpr 0) { oMouth.iSpr = 4; break; case 39: // Right key // update sprite positions if (iSel == 0) { oHead.iSpr++; if (oHead.iSpr = 4) { oHead.iSpr = 0; if (iSel == 1) { oEye.iSpr++; if (oEye.iSpr = 5) { oEye.iSpr = 0; if (iSel == 2) { oNose.iSpr++; if (oNose.iSpr = 5) { oNose.iSpr = 0; if (iSel == 3) { oMouth.iSpr++; if (oMouth.iSpr = 5) { oMouth.iSpr = 0; break; case 32: // Spacebar key - export results var temp_ctx, temp_canvas; temp_canvas = document.createElement( canvas  temp_ctx = temp_canvas.getContext( 2d  temp_canvas.width = 360; temp_canvas.height = 410; // draw head temp_ctx.drawImage(oHead.image, oHead.iSpr*oHead.w, oHead.y2, oHead.w, oHead.h, oHead.x, oHead.y, oHead.w, oHead.h); // draw eyes temp_ctx.drawImage(oEye.image, oEye.iSpr*oEye.w, oEye.y2, oEye.w, oEye.h, oEye.x, oEye.y, oEye.w, oEye.h); // draw nose temp_ctx.drawImage(oNose.image, oNose.iSpr*oNose.w, oNose.y2, oNose.w, oNose.h, oNose.x, oNose.y, oNose.w, oNose.h); // draw mouth temp_ctx.drawImage(oMouth.image, oMouth.iSpr*oMouth.w, oMouth.y2, oMouth.w, oMouth.h, oMouth.x, oMouth.y, oMouth.w, oMouth.h); var vData = temp_canvas.toDataURL(); $( #face_result ).attr( src , vData); break; });  setInterval(drawScene, 40); // loop drawScene});

【相關推薦】

1. 免費h5在線視頻教程

2. HTML5 完整版手冊

3. VeVb.com原創html5視頻教程

以上就是分享利用 HTML5 的 Canvas 制作人臉的實例代碼的詳細內容,其它編程語言

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 辽阳市| 沈阳市| 疏附县| 贺州市| 都江堰市| 无棣县| 呼图壁县| 买车| 定襄县| 洛浦县| 木兰县| 翁源县| 六盘水市| 双鸭山市| 平顶山市| 黄冈市| 伊川县| 凤城市| 贵阳市| 新野县| 绥化市| 湖南省| 龙游县| 通道| 宜春市| 苗栗市| 垣曲县| 舟曲县| 新余市| 清涧县| 敖汉旗| 德惠市| 宁武县| 日土县| 博白县| 柳江县| 科技| 新兴县| 汨罗市| 增城市| 汨罗市|