请老师检查,代码怎么修改能避免输入框只输入空格导致绘制不出文字的情况?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | <!DOCTYPE html> < html > < head > < meta charset = "UTF-8" > < title >canvas案例</ title > < style > .content{ width: 1000px; height: 285px; margin: 100px auto; } .input-content{ width: 300px; height: 248px; background-color: #a4a19a; padding-top: 37px; float: left; } .text{ width: 255px; height: 23px; font-size: 10px; background-color: #fff; padding-left: 10px; border: none; border-radius: 12px; outline: none; margin: 0 18px 22px; } .submit{ width: 84px; height: 23px; font-size: 10px; background-color: #232124; border-radius: 12px; border: none; outline: none; color: #fff; margin-left: 108px; cursor: pointer; } .canvas-content{ width: 700px; height: 100%; background-color: #f0e9d6; float: left; } .card{ margin: 75px 100px; } </ style > </ head > < body > < div class = "content" > < div class = "input-content" > < input type = "text" class = "text" placeholder = "姓名" id = "name" autocomplete = "off" maxlength = "6" > < input type = "text" class = "text" placeholder = "地址" id = "adds" autocomplete = "off" maxlength = "10" > < input type = "text" class = "text" placeholder = "职业" id = "work" autocomplete = "off" maxlength = "10" > < input type = "text" class = "text" placeholder = "口号" id = "slogan" autocomplete = "off" maxlength = "10" > < button class = "submit" id = "subBtn" >生成名片</ button > </ div > < div class = "canvas-content" > < canvas class = "card" id = "canvas" width = "500px" height = "76px" > 您的浏览器不支持canvas </ canvas > </ div > </ div > < script > var canvas = document.getElementById("canvas"); var ctx = canvas.getContext('2d'); var btn = document.getElementById('subBtn'), nameNode = document.getElementById('name'), addsNode = document.getElementById('adds'), workNode = document.getElementById('work'), sloganNode = document.getElementById('slogan'); function draw() { var nameStr = nameNode.value || '请输入姓名', addsStr = addsNode.value || '请输入地址', workStr = workNode.value || '请输入职业', sloganStr = sloganNode.value || '请输入口号'; //创建canvas渐变色背景 var linearGradient = ctx.createLinearGradient(0,38,500,38); linearGradient.addColorStop(0,'#000'); linearGradient.addColorStop(.6,'#000'); linearGradient.addColorStop(1,'grey'); ctx.fillStyle = linearGradient; ctx.fillRect(0,0,canvas.width,canvas.height); //设置图片 var img = new Image(); img.src = 'http://img1.sycdn.imooc.com/climg//59859f4d00014f1503000100.jpg'; img.onload = function () { ctx.drawImage(img,20,11,80,81,0,0,78,78); } //设置文字 ctx.fillStyle = '#fff'; ctx.textAlign = 'left'; ctx.textBaseline = 'top'; ctx.save(); ctx.font = 'bold 22px 微软雅黑'; ctx.fillText(nameStr,93,10); ctx.restore(); ctx.save(); ctx.font = 'bold 14px 微软雅黑'; ctx.fillText(addsStr,93,37); ctx.fillText(workStr,93,56); var nameStrWidth = ctx.measureText(nameStr).width, sloganPositionX = 245, sloganX = nameStrWidth + sloganPositionX; //移动中心点 设置旋转 设置旋转文字 ctx.translate(sloganX,37); ctx.rotate(- Math.PI / 30); ctx.fillText(sloganStr,0,0); ctx.restore(); } draw(); //设置btn点击事件 btn.onclick = function () { ctx.clearRect(0, 0, canvas.width, canvas.height); draw(); }; </ script > </ body > </ html > |
16
收起
正在回答
2回答
同学你好,若是使用多个空格的话,可以使用正则,用正则将空格替换为空(什么都没有),然后什么都没有在if判断的时候会为假,例:
祝学习愉快~
3.WebAPP开发与小程序
- 参与学习 人
- 提交作业 622 份
- 解答问题 6815 个
微信带火了小程序,也让前端工程师有了更多的展现机会,本阶段带你从移动基础知识的学习到webAPP开发,及小程序开发,让你PC端与移动端两端通吃。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧