用html5实现如图字中有字效果
实现思路:用canvas输出文字,然后分析像素点,根据像素点输出文字。
核心代码:
JavaScript Code复制内容到剪贴板
- var canvas;
-
var ctx;
-
var tex;
-
var blankStr = " ";
-
-
$(function () {
-
-
$("button#bt").click(function () {
-
-
if ($("canvas#myCanvas").length > 0) {
-
canvas = $("canvas#myCanvas")[0];
-
ctx = canvas.getContext("2d");
- }
-
-
var tex = $("input#inpt").val();
-
if (!tex) tex = "顶";
-
if (tex.length > 1) {
-
alert("亲,少输入点字好吧,我吃不消呢~");
-
return;
- }
-
-
var reg = /[\u4E00-\u9FA5]/g;
-
if (!reg.test(tex)) {
-
alert("亲,输入一个汉字好吧,其他的我不认识呢~");
-
return;
- }
-
-
ctx.fillStyle = "rgba(0,0,0,1)";
- ctx.fillRect(20, 20, 40, 40);
- ?
-
ctx.fillStyle = "rgba(255,255,255,1)"
-
ctx.font = "bolder 40px 宋体";
-
ctx.textBaseline = 'top';
- ctx.fillText(tex, 20, 20);
-
-
var panel = $("#panel");
-
panel.html("");
-
-
for (y = 1; y
-
for (x = 1; x
-
imageData = ctx.getImageData(20 + x, 20 + y, 1, 1);
-
if (imageData.data[0] >180 ) {
- panel.html(panel.html() + blankStr);
-
} else {
- panel.html(panel.html() + tex);
- }
- }
-
panel.html(panel.html() + "
");
- }
- });
-
-
$("button#bt").click();
- });
以上这篇字中字效果的实现【html5实例】就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
原文地址:http://www.cnblogs.com/shouce/archive/2016/05/03/5453737.html
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn