动态生成的网站图标
是否可以仅使用 JavaScript 和 HTML 来动态生成 favicon,使用当前页面的 favicon 作为背景,并在前景中使用随机数?
例如,假设当前的图标看起来与此类似:
======================
====XXXXXXXXXXXXXX====
====X=================
====X=================
====X=====XXXXXXXX====
====X============X====
====X============X====
====XXXXXXXXXXXXXX====
======================
如果可能,我如何仅使用 JavaScript 和 HTML 使其看起来与此类似:
======================
====XXXXXXXXXXXXXX====
====X=================
====X=================
====X=====XXXXXXXX====
====X=========--111--=
====X=========--1-1--=
====XXXXXXXXXX----1--=
==============--1111-=
map:=
:白色背景x
:原始 Favicon 图像-
:红色生成的带有数字的图像1
:白色文本
想法:
- 画布?
- 数据 Uri 的?
Would it be possible using only JavaScript and HTML to dynamically generate a favicon, using the current page's favicon as a background, and a random number in the foreground?
For example, lets say the current favicon looks similar to this:
======================
====XXXXXXXXXXXXXX====
====X=================
====X=================
====X=====XXXXXXXX====
====X============X====
====X============X====
====XXXXXXXXXXXXXX====
======================
If possible, how would I get it to look something similar to this using only JavaScript and HTML:
======================
====XXXXXXXXXXXXXX====
====X=================
====X=================
====X=====XXXXXXXX====
====X=========--111--=
====X=========--1-1--=
====XXXXXXXXXX----1--=
==============--1111-=
map:=
: white backgroundx
: Original Favicon image-
: Red generated image with a number1
: White text
Ideas:
- Canvas?
- Data Uri's?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑:让它工作
你实际上可以运行 chrome 并将其粘贴
到浏览器中并查看它的运行情况。
EDIT: Got it working with
You can actually run chrome and paste this:
into the browser and see it in action.
您可以查看此问题,其中讨论了如何动态更改网站图标。另外这里有一个网站,声称仅使用 JavaScript、canvas 和数据 URI,它应该适用于除 IE 之外的所有现代浏览器。不确定这是否能满足您的要求,但您可以尝试对其工作原理进行逆向工程。
这是一个SO答案,解释了如何使用canvas元素读取favicon数据并取出图像数据,然后可以对其进行修改并组合成新的图标。
You might take a look at this question, which discusses how to change the favicon dynamically. Also here is a site that claims to play a game in the favicon using only JavaScript, canvas, and the data URI, which should work in all modern browsers except IE. Not sure if that would fulfill your requirements or not, but you can try reverse engineering how it works.
Here's an SO answer that explains how to use the canvas element to read the favicon data and get the image data out, which then can be modified and composed into a new icon.
我不知道如何在浏览器中完成这一切,但很容易有一个服务器端点接受 URL 中的参数并返回一个组合的图标。然后 Javascript 可以修改 favicon url 来获取它想要的图像。
I don't know about doing it all in the browser, but it would be easy to have a server endpoint that accepted parameters in the URL and returned a composed favicon. Then Javascript could modify the favicon url to get the image it wanted.
favicon.js 正是这样做的。除其他功能外,该库还可以在图标的右下角显示一个计数器。
favicon.js does exactly this. This library can display a counter in the bottom right corner of the favicon, among other features.