Retina Monitor’s Web Image Solution

什麼是Retina螢幕


今天來談談網頁圖片在視網膜(Retina)螢幕的顯示議題,首先必須來談談什麼是視網膜螢幕,其實他就是一種能在單位尺寸內顯示更多像素點的螢幕,且已經達到視網膜能辨識的極限,視網膜螢幕是由蘋果公司提出,賈伯斯在2010年iPhone 4的發表會中提到:「當你所拿的東西距離你10-12英寸(約25-30厘米)時,它的解析度只要達到300ppi這個『神奇數字』(每英寸300個像素點)以上,你的視網膜就無法分辨出像素點了。」

所以簡言之,視網膜螢幕比較一般非視網膜螢幕的不同處在於,能在同樣的範圍顯示更多的像素點,也就是PPI(Pixels Per Inch)比較高的螢幕,可參考下圖所示:

可以看到圖左在單位尺寸內顯示的像素點較少,圖右則能以更多的像素去呈現,在肉眼上就會覺得圖右會比較細緻。

接下來談談網頁圖片與視網膜螢幕之間的關係,先來看看同樣一張圖片在不同螢幕上顯示的效果。

非Retina顯示

Retina顯示

當然在美觀的立場下,我們不會希望發生這樣的視覺差異,那網頁上要如何設置才可以做到讓圖形在視網膜螢幕下也能正常的顯示呢?以下提出一些解決方案:

方案1. 透過CSS針對不同的DPR (Device Pixel Ratio)設計顯示圖片


大家如果想知道自己螢幕的DPR,可以在Chrome的主控台輸入“window.devicePixelRatio”,就可以得知目前Chrome所在螢幕的DPR值。

非Retina螢幕
Retina螢幕

可以觀察出來在兩個螢幕的DRP值是不同的,而透過CSS的樣式,可以針對這兩個不同的DPR設定顯示方式,可以參考下面的做法:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .logo{
            width: 190px;
            height: 190px;
            background-image: url("atd-icon.jpg");
            background-size: contain;
        }
        @media (-webkit-min-device-pixel-ratio: 2){
            .logo{
                background-image: url("atd-icon@2x.jpg");
            }
        }
    </style>
</head>
<body>
    <div class="logo"></div>
</body>
</html>
<script>
</script>

這邊必須針對圖檔尺寸做個說明,其中atd-icon.jpg為190 x 190,而atd-icon@2x.jpg為380 x 380。

@media (-webkit-min-device-pixel-ratio: 2)的部分是在DPR兩倍下的Media Query。

透過這樣的方式去設定,當DPR為2的時候,螢幕上的圖片改變為atd-icon_2.jpg,剛好能夠滿足在兩倍像素狀況下的顯示需求,當然以此類推,也能去設定三倍或更高倍數下的顯示圖片。

方案二. 透過img標籤的srcset屬性


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <img src="atdIcon.jpg" srcset="atd-icon@2x.jpg x2">
</body>
</html>
<script>
</script>

其中srcset=”atd-icon@2x.jpg 2x”表示在DPR等於2的時候要去呈現的圖片。

非Retina螢幕
Retina螢幕

以上兩個方法都可以解決圖片在Retina螢幕上面顯示的問題,但也要稍微注意一下支援程度:

CSS -webkit-min-device-pixel-ratio 支援列表
HTML srcset 支援列表

可以看到在IE都是無法支援的狀態,但從Edge後就開始支援,當然也可以想想目前擁有Retina螢幕的使用者,用的瀏覽器都會是哪些?說不定你不會太在乎上面那兩張圖紅色的部分。

但若是一定要有IE瀏覽器的支援,大家應該也能從上述的文章了解,我們可以透過window.devicePixelRatio的數值自行寫script進行判斷,然後再給出適合DPR的圖片,不過這邊另外推薦一個JS Library可以協助你進行這方面的判斷。

方案三. 透過Retina.js輔助進行判斷


首先到Retina.js的網站下載他們所提供的js檔,接下來至網頁引入該JS,其實網站上也有詳細的使用方法,另外檔案大小僅有約4KB並不算很大,可以參考下面的Sample Code。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="retina.min.js"></script>
</head>
<body>
    <img src="atd-icon.jpg" />
</body>
</html>

可以看到在img標籤中僅使用了原始大小的atd-icon.jpg,但因為有引入retina.min.js,他會在螢幕DPR兩倍時,自動去搜尋符合倍率下的圖片,但要記得如果不加其他屬性去設定兩倍圖片的檔名,檔名就必須按照內建的規則,也就是atd-icon@2x.jpg,當然3x也依然支援,可以到官網看一下他的使用方式,但若是使用CSS設定的背景,依然沒辦法透過retina.js達到支援IE的效果。

以上三個方法提供目前在設計網頁時,對於Retina螢幕支援的做法,若在不考慮IE瀏覽器的情況下,其實方法1+方法2就已經可以解決所有情況,但若要支援IE則可以參考方法三的做法,也歡迎大家有問題可以留言進行討論。

方案四. 使用SVG圖檔


過往我們在網頁中使用的圖片僅有GIF、PNG、JPG這三種點陣圖格式,所以在高解析度(Retina)的螢幕中,我們需要輸入不同Size的圖片,來補足像素點不足的問題,現在有了另外一種解法“SVG”圖檔。

大家可以把SVG視為一種可以放在網頁上的向量圖檔,也就是說我們以前透過Illustrator或其他向量繪圖軟體(也包含Photoshop中向量工具)繪製出來的向量圖,本身放大就不會失真,但因為要放在網頁上,我們把它轉換成了前述的三種點陣格式,才導致圖檔放大後的失真,而SVG就是保留圖片原來的向量屬性,簡言之,就是把向量圖檔放在網頁上的意思。

非Retina螢幕
Retina螢幕

感覺這個解決方案應該會比前幾項方便,但也有他必須要注意的地方:

  1. 在SVG檔案中匯入的點陣圖,在儲存成SVG時並不會被向量化,例如上圖中間的ASUS Design Logo就是匯入的點陣圖(放大後還是會模糊)。
  2. SVG字型可能會遺失,所以需要使用網路字型或是將文字轉成外框或形狀之後再轉存成SVG。
字型遺失
轉換成外框或形狀

You may also like...

36,522 Responses

  1. DavidCak表示:

    Overbets.ru предлагает вашему вниманию информацию о букмекерских конторах со всего мира. https://overbets.ru/

  2. Dwightkak表示:

    USACanadaPharm: canadian 24 hour pharmacy – USACanadaPharm

  3. TimothyDok表示:

    USACanadaPharm usa canada pharm usa canada pharm

  4. debet表示:

    There most be a solution for this problem, some people think there will be now solutions, but i think there wil be one.

  5. Dwightkak表示:

    canadianpharmacyworld: legal canadian pharmacy online – usa canada pharm

  6. Jamesced表示:

    Buy PackMan, Donuts, Muha Meds, Fryd vapes, which contain liquid with THC concentrate. Buy purified cannabis distillate (oil), containing from 70 to 90% THC. It is made from hemp inflorescences and its pollen. No chemistry, only a natural product. Only here you will find original electronic cigarettes with cannabis concentrate at the lowest prices.
    https://telegra.ph/Terpsolate-Terpsolyat-04-17

  7. Albertbiave表示:

    https://usacanadapharm.shop/# canadapharmacyonline legit

  8. StephenCer表示:

    canadian neighbor pharmacy: canada drug pharmacy – reputable canadian pharmacy

  9. Dwightkak表示:

    usa canada pharm: best canadian pharmacy online – USACanadaPharm

  10. Albertbiave表示:

    https://usacanadapharm.com/# certified canadian pharmacy

  11. Samuelcuh表示:

    USACanadaPharm: best canadian pharmacy – best canadian pharmacy to order from

  12. Samuelcuh表示:

    legal canadian pharmacy online: USACanadaPharm – northwest canadian pharmacy

  13. StephenCer表示:

    USACanadaPharm: usa canada pharm – usa canada pharm

  14. StephenCer表示:

    cheapest pharmacy canada: usa canada pharm – canadian drug pharmacy

  15. Gabrielnoind表示:

    Axolotl problems
    As Mexico City grew and became more industrialized, the need for water brought pumps and pipes to the lake, and eventually, “it was like a bad, smelly pond with rotten water,” Zambrano said. “All of our aquatic animals suffer with bad water quality, but amphibians suffer more because they have to breathe with the skin.”
    гей секс порно
    To add to the axolotls’ problems, invasive fish species such as carp and tilapia were introduced to the lake, where they feed on axolotl eggs. And a 1985 earthquake in Mexico City displaced thousands of people, who found new homes in the area around the lake, further contributing to the destruction of the axolotls’ habitat.

    These combined threats have devastated axolotl populations. According to the International Union for Conservation of Nature, there are fewer than 100 adult axolotls left in the wild. The species is considered critically endangered.
    While the wild axolotls of Lake Xochimilco have dwindled to near-extinction, countless axolotls have been bred for scientific laboratories and the pet trade. “The axolotl essentially helped establish the field of experimental zoology,” Voss said.

    In 1864, a French army officer brought live axolotls back to Europe, where scientists were surprised to learn that the seemingly juvenile aquatic salamanders were capable of reproduction. Since then, scientists around the world have studied axolotls and their DNA to learn about the salamanders’ unusual metamorphosis (or lack thereof) as well as their ability to regrow injured body parts.
    In addition to their role in labs, axolotls have become popular in the exotic pet trade (though they are illegal to own in California, Maine, New Jersey and Washington, DC). However, the axolotls you might find at a pet shop are different from their wild relatives in Lake Xochimilco. Most wild axolotls are a dark grayish brown. The famous pink axolotls, as well as other color variants such as white, blue, yellow and black, are genetic anomalies that are rare in the wild but selectively bred for in the pet trade.

    What’s more, “most of the animals in the pet trade have a very small genetic variance,” Zambrano said. Pet axolotls tend to be inbred and lack the wide flow of different genes that makes up a healthy population in the wild. That means that the axolotl extinction crisis can’t simply be solved by dumping pet axolotls into Lake Xochimilco. (Plus, the pet axolotls likely wouldn’t fare well with the poor habitat conditions in the lake.)

    Fame and misfortune
    The difficulties that axolotls face in the wild are almost diametrically opposed to the fame they’ve found in recent years. Axolotls have captured the human imagination for centuries, as evidenced by their roles in Aztec religion and stories, but the early 21st century seems to be a high point for them. An axolotl graces the 50 peso bill. There are axolotl-inspired Pokemon, and Reddit commenters have noted that the character Toothless from the “How to Train Your Dragon” movie series is distinctly axolotl-like.

    The introduction of axolotls to Minecraft in 2021 neatly mapped onto an uptick in Google searches for the animals, and social media makes it easy for people to gain access to photos and videos of the salamanders, particularly the photogenic pink ones often kept as pets.

    The axolotl pet trade probably doesn’t directly harm the wild populations since wild salamanders aren’t being poached or taken from Lake Xochimilco. However, Zambrano said, axolotls’ ubiquity in pop culture and pet stores might make people assume that because axolotls “live in all the tanks around the world, they are not in danger.”

  16. Allanbup表示:

    Why axolotls seem to be everywhere — except in the one lake they call home
    порно жесток бесплатно

    Scientist Dr. Randal Voss gets the occasional reminder that he’s working with a kind of superstar. When he does outreach events with his laboratory, he encounters people who are keen to meet his research subjects: aquatic salamanders called axolotls.

    The amphibians’ fans tell Voss that they know the animals from the internet, or from caricatures or stuffed animals, exclaiming, “‘They’re so adorable, we love them,’” said Voss, a professor of neuroscience at the University of Kentucky College of Medicine. “People are drawn to them.”

    Take one look at an axolotl, and it’s easy to see why it’s so popular. With their wide eyes, upturned mouths and pastel pink coloring, axolotls look cheerful and vaguely Muppet-like.

    They’ve skyrocketed in pop culture fame, in part thanks to the addition of axolotls to the video game Minecraft in 2021. These unusual salamanders are now found everywhere from Girl Scout patches to hot water bottles. But there’s more to axolotls than meets the eye: Their story is one of scientific discovery, exploitation of the natural world, and the work to rebuild humans’ connection with nature.

    A scientific mystery
    Axolotl is a word from Nahuatl, the Indigenous Mexican language spoken by the Aztecs and an estimated 1.5 million people today. The animals are named for the Aztec god Xolotl, who was said to transform into a salamander. The original Nahuatl pronunciation is “AH-show-LOAT”; in English, “ACK-suh-LAHT-uhl” is commonly used.
    Axolotls are members of a class of animals called amphibians, which also includes frogs. Amphibians lay their jelly-like eggs in water, and the eggs hatch into water-dwelling larval states. (In frogs, these larvae are called tadpoles.)

    Most amphibians, once they reach adulthood, are able to move to land. Since they breathe, in part, by absorbing oxygen through their moist skin, they tend to stay near water.

    Axolotls, however, never complete the metamorphosis to a land-dwelling adult form and spend their whole lives in the water.

    “They maintain their juvenile look throughout the course of their life,” Voss said. “They’re teenagers, at least in appearance, until they die.”

  17. Samuelcuh表示:

    USACanadaPharm: USACanadaPharm – usa canada pharm

  18. Dwightkak表示:

    legit canadian pharmacy online: prescription drugs canada buy online – USACanadaPharm

  19. Dwightkak表示:

    canadian pharmacy prices: USACanadaPharm – USACanadaPharm

  20. Georgefig表示:

    ссылка на сайт водкабет

  21. Albertbiave表示:

    https://usacanadapharm.shop/# usa canada pharm

  22. TimothyDok表示:

    canadianpharmacymeds com usa canada pharm canada drugstore pharmacy rx

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。