主頁 > 企業開發 > D3 Scale functions

D3 Scale functions

2020-09-14 10:55:35 企業開發

https://www.d3indepth.com/scales/

D3 in Depth Home About Introduction to D3 Selections Joins Enter/exit Scales Shapes Layouts Force Geographic Requests Transitions Interaction

Scale functions

Scale functions are JavaScript functions that:

  • take an input (usually a number, date or category) and
  • return a value (such as a coordinate, a colour, a length or a radius)

They’re typically used to transform (or ‘map’) data values into visual variables (such as position, length and colour).

For example, suppose we have some data:

[ 0, 2, 3, 5, 7.5, 9, 10 ]

we can create a scale function using:

var myScale = d3.scaleLinear()
  .domain([0, 10])
  .range([0, 600]);

D3 creates a function myScale which accepts input between 0 and 10 (the domain) and maps it to output between 0 and 600 (the range).

We can use myScale to calculate positions based on the data:

myScale(0);   // returns 0
myScale(2);   // returns 120
myScale(3);   // returns 180
...
myScale(10);  // returns 600
<iframe src="https://www.d3indepth.com/blocks/scales/example/" marginwidth="0" marginheight="0" scrolling="yes" width="320" height="240"></iframe> View source | Edit in GistRun

Scales are mainly used for transforming data values to visual variables such as position, length and colour.

For example they can transform:

  • data values into lengths between 0 and 500 for a bar chart
  • data values into positions between 0 and 200 for line charts
  • % change data (+4%, +10%, -5% etc.) into a continuous range of colours (with red for negative and green for positive)
  • dates into positions along an x-axis.

Constructing scales

(In this section we’ll just focus on linear scales as these are the most commonly used scale type. We’ll cover other types later on.)

To create a linear scale we use:

var myScale = d3.scaleLinear();
Version 4 uses a different naming convention to v3. We use d3.scaleLinear() in v4 and d3.scale.linear() in v3.

As it stands the above function isn’t very useful so we can configure the input bounds (the domain) as well as the output bounds (the range):

myScale
  .domain([0, 100])
  .range([0, 800]);

Now myScale is a function that accepts input between 0 and 100 and linearly maps it to between 0 and 800.

myScale(0);    // returns 0
myScale(50);   // returns 400
myScale(100);  // returns 800
Try experimenting with scale functions by copying code fragments and pasting them into the console or using a web-based editor such as JS Bin.

D3 scale types

D3 has around 12 different scale types (scaleLinear, scalePow, scaleQuantise, scaleOrdinal etc.) and broadly speaking they can be classified into 3 groups:

  • scales with continuous input and continuous output
  • scales with continuous input and discrete output
  • scales with discrete input and discrete output

We’ll now look at these 3 categories one by one.

Scales with continuous input and continuous output

In this section we cover scale functions that map from a continuous input domain to a continuous output range.

scaleLinear

Linear scales are probably the most commonly used scale type as they are the most suitable scale for transforming data values into positions and lengths. If there’s one scale type to learn about this is the one.

They use a linear function (y = m * x + b) to interpolate across the domain and range.

var linearScale = d3.scaleLinear()
  .domain([0, 10])
  .range([0, 600]);

linearScale(0);   // returns 0
linearScale(5);   // returns 300
linearScale(10);  // returns 600

Typical uses are to transform data values into positions and lengths, so when creating bar charts, line charts (as well as many other chart types) they are the scale to use.

The output range can also be specified as colours:

var linearScale = d3.scaleLinear()
  .domain([0, 10])
  .range(['yellow', 'red']);

linearScale(0);   // returns "rgb(255, 255, 0)"
linearScale(5);   // returns "rgb(255, 128, 0)"
linearScale(10);  // returns "rgb(255, 0, 0)"

This can be useful for visualisations such as choropleth maps, but also consider scaleQuantizescaleQuantile and scaleThreshold.

scalePow

More included for completeness, rather than practical usefulness, the power scale interpolates using a power (y = m * x^k + b) function. The exponent k is set using .exponent():

var powerScale = d3.scalePow()
  .exponent(0.5)
  .domain([0, 100])
  .range([0, 30]);

powerScale(0);   // returns 0
powerScale(50);  // returns 21.21...
powerScale(100); // returns 30

scaleSqrt

The scaleSqrt scale is a special case of the power scale (where k = 0.5) and is useful for sizing circles by area (rather than radius). (When using circle size to represent data, it’s considered better practice to set the area, rather than the radius proportionally to the data.)

var sqrtScale = d3.scaleSqrt()
  .domain([0, 100])
  .range([0, 30]);

sqrtScale(0);   // returns 0
sqrtScale(50);  // returns 21.21...
sqrtScale(100); // returns 30
<iframe src="https://www.d3indepth.com/blocks/scales/sqrt/" marginwidth="0" marginheight="0" scrolling="yes" width="320" height="240"></iframe> View source | Edit in GistRun

scaleLog

Log scales interpolate using a log function (y = m * log(x) + b) and can be useful when the data has an exponential nature to it.

var logScale = d3.scaleLog()
  .domain([10, 100000])
  .range([0, 600]);

logScale(10);     // returns 0
logScale(100);    // returns 150
logScale(1000);   // returns 300
logScale(100000); // returns 600
<iframe src="https://www.d3indepth.com/blocks/scales/log/" marginwidth="0" marginheight="0" scrolling="yes" width="320" height="240"></iframe> View source | Edit in GistRun

scaleTime

scaleTime is similar to scaleLinear except the domain is expressed as an array of dates. (It’s very useful when dealing with time series data.)

timeScale = d3.scaleTime()
  .domain([new Date(2016, 0, 1), new Date(2017, 0, 1)])
  .range([0, 700]);

timeScale(new Date(2016, 0, 1));   // returns 0
timeScale(new Date(2016, 6, 1));   // returns 348.00...
timeScale(new Date(2017, 0, 1));   // returns 700
<iframe src="https://www.d3indepth.com/blocks/scales/time/" marginwidth="0" marginheight="0" scrolling="yes" width="320" height="240"></iframe> View source | Edit in GistRun

scaleSequential

scaleSequential is used for mapping continuous values to an output range determined by a preset (or custom) interpolator. (An interpolator is a function that accepts input between 0 and 1 and outputs an interpolated value between two numbers, colours, strings etc.)

D3 provides a number of preset interpolators including many colour ones. For example we can use d3.interpolateRainbow to create the well known rainbow colour scale:

var sequentialScale = d3.scaleSequential()
  .domain([0, 100])
  .interpolator(d3.interpolateRainbow);

sequentialScale(0);   // returns 'rgb(110, 64, 170)'
sequentialScale(50);  // returns 'rgb(175, 240, 91)'
sequentialScale(100); // returns 'rgb(110, 64, 170)'
<iframe src="https://www.d3indepth.com/blocks/scales/sequential/" marginwidth="0" marginheight="0" scrolling="yes" width="320" height="240"></iframe> View source | Edit in GistRun

Note that the interpolator determines the output range so you don’t need to specify the range yourself.

The example below shows some of the other colour interpolators provided by D3:

<iframe src="https://www.d3indepth.com/blocks/scales/sequential-options/" marginwidth="0" marginheight="0" scrolling="yes" width="320" height="240"></iframe> View source | Edit in GistRun

There’s also a plug-in d3-scale-chromatic which provides the well known ColorBrewer colour schemes.

Clamping

By default scaleLinearscalePowscaleSqrtscaleLogscaleTime and scaleSequential allow input outside the domain. For example:

var linearScale = d3.scaleLinear()
  .domain([0, 10])
  .range([0, 100]);

linearScale(20);  // returns 200
linearScale(-10); // returns -100

In this instance the scale function uses extrapolation for values outside the domain.

If we’d like the scale function to be restricted to input values inside the domain we can ‘clamp’ the scale function using .clamp():

linearScale.clamp(true);

linearScale(20);  // returns 100
linearScale(-10); // returns 0

We can switch off clamping using .clamp(false).

Nice

If the domain has been computed automatically from real data (e.g. by using d3.extent) the start and end values might not be round figures. This isn’t necessarily a problem, but if using the scale to define an axis, it can look a bit untidy:

var data = [0.243, 0.584, 0.987, 0.153, 0.433];
var extent = d3.extent(data);

var linearScale = d3.scaleLinear()
  .domain(extent)
  .range([0, 100]);
<iframe src="https://www.d3indepth.com/blocks/scales/axis/" marginwidth="0" marginheight="0" scrolling="yes" width="320" height="240"></iframe> View source | Edit in GistRun

Therefore D3 provides a function .nice() on the scales in this section which will round the domain to ‘nice’ round values:

linearScale.nice();
<iframe src="https://www.d3indepth.com/blocks/scales/nice/" marginwidth="0" marginheight="0" scrolling="yes" width="320" height="240"></iframe> View source | Edit in GistRun

Note that .nice() must be called each time the domain is updated.

Multiple segments

The domain and range of scaleLinearscalePowscaleSqrtscaleLog and scaleTime usually consists of two values, but if we provide 3 or more values the scale function is subdivided into multiple segments:

var linearScale = d3.scaleLinear()
  .domain([-10, 0, 10])
  .range(['red', '#ddd', 'blue']);

linearScale(-10);  // returns "rgb(255, 0, 0)"
linearScale(0);    // returns "rgb(221, 221, 221)"
linearScale(5);    // returns "rgb(111, 111, 238)"
<iframe src="https://www.d3indepth.com/blocks/scales/multisegment/" marginwidth="0" marginheight="0" scrolling="yes" width="320" height="240"></iframe> View source | Edit in GistRun

Typically multiple segments are used for distinguishing between negative and positive values (such as in the example above). We can use as many segments as we like as long as the domain and range are of the same length.

Inversion

The .invert() method allows us to determine a scale function’s input value given an output value (provided the scale function has a numeric domain):

var linearScale = d3.scaleLinear()
  .domain([0, 10])
  .range([0, 100]);

linearScale.invert(50);   // returns 5
linearScale.invert(100);  // returns 10

A common use case is when we want to convert a user’s click along an axis into a domain value:

<iframe src="https://www.d3indepth.com/blocks/scales/invert/" marginwidth="0" marginheight="0" scrolling="yes" width="320" height="240"></iframe> View source | Edit in GistRun

Scales with continuous input and discrete output

scaleQuantize

scaleQuantize accepts continuous input and outputs a number of discrete quantities defined by the range.

var quantizeScale = d3.scaleQuantize()
  .domain([0, 100])
  .range(['lightblue', 'orange', 'lightgreen', 'pink']);

quantizeScale(10);   // returns 'lightblue'
quantizeScale(30);  // returns 'orange'
quantizeScale(90);  // returns 'pink'

Each range value is mapped to an equal sized chunk in the domain so in the example above:

  • 0 ≤ u < 25 is mapped to ‘lightblue’
  • 25 ≤ u < 50 is mapped to ‘orange’
  • 50 ≤ u < 75 is mapped to ‘lightgreen’
  • 75 ≤ u < 100 is mapped to ‘pink’

where u is the input value.

<iframe src="https://www.d3indepth.com/blocks/scales/quantize/" marginwidth="0" marginheight="0" scrolling="yes" width="320" height="240"></iframe> View source | Edit in GistRun

Note also that input values outside the domain are clamped so in our example quantizeScale(-10) returns ‘lightblue’ and quantizeScale(110) returns ‘pink’.

scaleQuantile

scaleQuantile maps continuous numeric input to discrete values. The domain is defined by an array of numbers:

var myData = [0, 5, 7, 10, 20, 30, 35, 40, 60, 62, 65, 70, 80, 90, 100];

var quantileScale = d3.scaleQuantile()
  .domain(myData)
  .range(['lightblue', 'orange', 'lightgreen']);

quantileScale(0);   // returns 'lightblue'
quantileScale(20);  // returns 'lightblue'
quantileScale(30);  // returns 'orange'
quantileScale(65);  // returns 'lightgreen'
<iframe src="https://www.d3indepth.com/blocks/scales/quantile/" marginwidth="0" marginheight="0" scrolling="yes" width="320" height="240"></iframe> View source | Edit in GistRun

The (sorted) domain array is divided into n equal sized groups where n is the number of range values.

Therefore in the above example the domain array is split into 3 groups where:

  • the first 5 values are mapped to ‘lightblue’
  • the next 5 values to ‘orange’ and
  • the last 5 values to ‘lightgreen’.

The split points of the domain can be accessed using .quantiles():

quantileScale.quantiles();  // returns [26.66..., 63]

If the range contains 4 values quantileScale computes the quartiles of the data. In other words, the lowest 25% of the data is mapped to range[0], the next 25% of the data is mapped to range[1] etc.

scaleThreshold

scaleThreshold maps continuous numeric input to discrete values defined by the range. n-1 domain split points are specified where n is the number of range values.

In the following example we split the domain at 050 and 100

  • u < 0 is mapped to ‘#ccc’
  • 0 ≤ u < 50 to ‘lightblue’
  • 50 ≤ u < 100 to ‘orange’
  • u ≥ 100 to ‘#ccc’

where u is the input value.

var thresholdScale = d3.scaleThreshold()
  .domain([0, 50, 100])
  .range(['#ccc', 'lightblue', 'orange', '#ccc']);

thresholdScale(-10);  // returns '#ccc'
thresholdScale(20);   // returns 'lightblue'
thresholdScale(70);   // returns 'orange'
thresholdScale(110);  // returns '#ccc'
<iframe src="https://www.d3indepth.com/blocks/scales/threshold/" marginwidth="0" marginheight="0" scrolling="yes" width="320" height="240"></iframe> View source | Edit in GistRun

Scales with discrete input and discrete output

scaleOrdinal

scaleOrdinal maps discrete values (specified by an array) to discrete values (also specified by an array). The domain array specifies the possible input values and the range array the output values. The range array will repeat if it’s shorter than the domain array.

var myData = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']

var ordinalScale = d3.scaleOrdinal()
  .domain(myData)
  .range(['black', '#ccc', '#ccc']);

ordinalScale('Jan');  // returns 'black';
ordinalScale('Feb');  // returns '#ccc';
ordinalScale('Mar');  // returns '#ccc';
ordinalScale('Apr');  // returns 'black';
<iframe src="https://www.d3indepth.com/blocks/scales/ordinal/" marginwidth="0" marginheight="0" scrolling="yes" width="320" height="240"></iframe> View source | Edit in GistRun

By default if a value that’s not in the domain is used as input, the scale will implicitly add the value to the domain:

ordinalScale('Monday');  // returns 'black';

If this isn’t the desired behvaiour we can specify an output value for unknown values using .unknown():

ordinalScale.unknown('Not a month');
ordinalScale('Tuesday'); // returns 'Not a month'

D3 can also provide preset colour schemes (from ColorBrewer):

var ordinalScale = d3.scaleOrdinal()
  .domain(myData)
  .range(d3.schemePaired);
<iframe src="https://www.d3indepth.com/blocks/scales/ordinal-scheme/" marginwidth="0" marginheight="0" scrolling="yes" width="320" height="240"></iframe> View source | Edit in GistRun

(Note that the Brewer colour schemes are defined within a separate file d3-scale-chromatic.js.)

scaleBand

When creating bar charts scaleBand helps to determine the geometry of the bars, taking into account padding between each bar. The domain is specified as an array of values (one value for each band) and the range as the minimum and maximum extents of the bands (e.g. the total width of the bar chart).

In effect scaleBand will split the range into n bands (where n is the number of values in the domain array) and compute the positions and widths of the bands taking into account any specified padding.

var bandScale = d3.scaleBand()
  .domain(['Mon', 'Tue', 'Wed', 'Thu', 'Fri'])
  .range([0, 200]);

bandScale('Mon'); // returns 0
bandScale('Tue'); // returns 40
bandScale('Fri'); // returns 160

The width of each band can be accessed using .bandwidth():

bandScale.bandwidth();  // returns 40

Two types of padding may be configured:

  • paddingInner which specifies (as a percentage of the band width) the amount of padding between each band
  • paddingOuter which specifies (as a percentage of the band width) the amount of padding before the first band and after the last band

Let’s add some inner padding to the example above:

bandScale.paddingInner(0.05);

bandScale.bandWidth();  // returns 38.38...
bandScale('Mon');       // returns 0
bandScale('Tue');       // returns 40.40...

Putting this all together we can create this bar chart:

<iframe src="https://www.d3indepth.com/blocks/scales/band/" marginwidth="0" marginheight="0" scrolling="yes" width="320" height="240"></iframe> View source | Edit in GistRun

scalePoint

scalePoint creates scale functions that map from a discrete set of values to equally spaced points along the specified range:

var pointScale = d3.scalePoint()
  .domain(['Mon', 'Tue', 'Wed', 'Thu', 'Fri'])
  .range([0, 500]);

pointScale('Mon');  // returns 0
pointScale('Tue');  // returns 125
pointScale('Fri');  // returns 500
<iframe src="https://www.d3indepth.com/blocks/scales/point/" marginwidth="0" marginheight="0" scrolling="yes" width="320" height="240"></iframe> View source | Edit in GistRun

The distance between the points can be accessed using .step():

pointScale.step();  // returns 125

Outside padding can be specified as the ratio of the padding to point spacing. For example, for the outside padding to be a quarter of the point spacing use a value of 0.25:

pointScale.padding(0.25);

pointScale('Mon');  // returns 27.77...
pointScale.step();  // returns 111.11...

Further reading

ColorBrewer schemes for D3

Mike Bostock on d3-scale

轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/34150.html

標籤:JavaScript

上一篇:jQuery 原始碼分析(十九) DOM遍歷模塊詳解

下一篇:canvas 的 getImageData 和 toDataUrl 跨域問題

標籤雲
其他(157675) Python(38076) JavaScript(25376) Java(17977) C(15215) 區塊鏈(8255) C#(7972) AI(7469) 爪哇(7425) MySQL(7132) html(6777) 基礎類(6313) sql(6102) 熊猫(6058) PHP(5869) 数组(5741) R(5409) Linux(5327) 反应(5209) 腳本語言(PerlPython)(5129) 非技術區(4971) Android(4554) 数据框(4311) css(4259) 节点.js(4032) C語言(3288) json(3245) 列表(3129) 扑(3119) C++語言(3117) 安卓(2998) 打字稿(2995) VBA(2789) Java相關(2746) 疑難問題(2699) 细绳(2522) 單片機工控(2479) iOS(2429) ASP.NET(2402) MongoDB(2323) 麻木的(2285) 正则表达式(2254) 字典(2211) 循环(2198) 迅速(2185) 擅长(2169) 镖(2155) 功能(1967) .NET技术(1958) Web開發(1951) python-3.x(1918) HtmlCss(1915) 弹簧靴(1913) C++(1909) xml(1889) PostgreSQL(1872) .NETCore(1853) 谷歌表格(1846) Unity3D(1843) for循环(1842)

熱門瀏覽
  • IEEE1588PTP在數字化變電站時鐘同步方面的應用

    IEEE1588ptp在數字化變電站時鐘同步方面的應用 京準電子科技官微——ahjzsz 一、電力系統時間同步基本概況 隨著對IEC 61850標準研究的不斷深入,國內外學者提出基于IEC61850通信標準體系建設數字化變電站的發展思路。數字化變電站與常規變電站的顯著區別在于程序層傳統的電流/電壓互 ......

    uj5u.com 2020-09-10 03:51:52 more
  • HTTP request smuggling CL.TE

    CL.TE 簡介 前端通過Content-Length處理請求,通過反向代理或者負載均衡將請求轉發到后端,后端Transfer-Encoding優先級較高,以TE處理請求造成安全問題。 檢測 發送如下資料包 POST / HTTP/1.1 Host: ac391f7e1e9af821806e890 ......

    uj5u.com 2020-09-10 03:52:11 more
  • 網路滲透資料大全單——漏洞庫篇

    網路滲透資料大全單——漏洞庫篇漏洞庫 NVD ——美國國家漏洞庫 →http://nvd.nist.gov/。 CERT ——美國國家應急回應中心 →https://www.us-cert.gov/ OSVDB ——開源漏洞庫 →http://osvdb.org Bugtraq ——賽門鐵克 →ht ......

    uj5u.com 2020-09-10 03:52:15 more
  • 京準講述NTP時鐘服務器應用及原理

    京準講述NTP時鐘服務器應用及原理京準講述NTP時鐘服務器應用及原理 安徽京準電子科技官微——ahjzsz 北斗授時原理 授時是指接識訓通過某種方式獲得本地時間與北斗標準時間的鐘差,然后調整本地時鐘使時差控制在一定的精度范圍內。 衛星導航系統通常由三部分組成:導航授時衛星、地面檢測校正維護系統和用戶 ......

    uj5u.com 2020-09-10 03:52:25 more
  • 利用北斗衛星系統設計NTP網路時間服務器

    利用北斗衛星系統設計NTP網路時間服務器 利用北斗衛星系統設計NTP網路時間服務器 安徽京準電子科技官微——ahjzsz 概述 NTP網路時間服務器是一款支持NTP和SNTP網路時間同步協議,高精度、大容量、高品質的高科技時鐘產品。 NTP網路時間服務器設備采用冗余架構設計,高精度時鐘直接來源于北斗 ......

    uj5u.com 2020-09-10 03:52:35 more
  • 詳細解讀電力系統各種對時方式

    詳細解讀電力系統各種對時方式 詳細解讀電力系統各種對時方式 安徽京準電子科技官微——ahjzsz,更多資料請添加VX 衛星同步時鐘是我京準公司開發研制的應用衛星授時時技術的標準時間顯示和發送的裝置,該裝置以M國全球定位系統(GLOBAL POSITIONING SYSTEM,縮寫為GPS)或者我國北 ......

    uj5u.com 2020-09-10 03:52:45 more
  • 如何保證外包團隊接入企業內網安全

    不管企業規模的大小,只要企業想省錢,那么企業的某些服務就一定會采用外包的形式,然而看似美好又經濟的策略,其實也有不好的一面。下面我通過安全的角度來聊聊使用外包團的安全隱患問題。 先看看什么服務會使用外包的,最常見的就是話務/客服這種需要大量重復性、無技術性的服務,或者是一些銷售外包、特殊的職能外包等 ......

    uj5u.com 2020-09-10 03:52:57 more
  • PHP漏洞之【整型數字型SQL注入】

    0x01 什么是SQL注入 SQL是一種注入攻擊,通過前端帶入后端資料庫進行惡意的SQL陳述句查詢。 0x02 SQL整型注入原理 SQL注入一般發生在動態網站URL地址里,當然也會發生在其它地發,如登錄框等等也會存在注入,只要是和資料庫打交道的地方都有可能存在。 如這里http://192.168. ......

    uj5u.com 2020-09-10 03:55:40 more
  • [GXYCTF2019]禁止套娃

    git泄露獲取原始碼 使用GET傳參,引數為exp 經過三層過濾執行 第一層過濾偽協議,第二層過濾帶引數的函式,第三層過濾一些函式 preg_replace('/[a-z,_]+\((?R)?\)/', NULL, $_GET['exp'] (?R)參考當前正則運算式,相當于匹配函式里的引數 因此傳遞 ......

    uj5u.com 2020-09-10 03:56:07 more
  • 等保2.0實施流程

    流程 結論 ......

    uj5u.com 2020-09-10 03:56:16 more
最新发布
  • 使用Django Rest framework搭建Blog

    在前面的Blog例子中我們使用的是GraphQL, 雖然GraphQL的使用處于上升趨勢,但是Rest API還是使用的更廣泛一些. 所以還是決定回到傳統的rest api framework上來, Django rest framework的官網上給了一個很好用的QuickStart, 我參考Qu ......

    uj5u.com 2023-04-20 08:17:54 more
  • 記錄-new Date() 我忍你很久了!

    這里給大家分享我在網上總結出來的一些知識,希望對大家有所幫助 大家平時在開發的時候有沒被new Date()折磨過?就是它的諸多怪異的設定讓你每每用的時候,都可能不小心踩坑。造成程式意外出錯,卻一下子找不到問題出處,那叫一個煩透了…… 下面,我就列舉它的“四宗罪”及應用思考 可惡的四宗罪 1. Sa ......

    uj5u.com 2023-04-20 08:17:47 more
  • 使用Vue.js實作文字跑馬燈效果

    實作文字跑馬燈效果,首先用到 substring()截取 和 setInterval計時器 clearInterval()清除計時器 效果如下: 實作代碼如下: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta ......

    uj5u.com 2023-04-20 08:12:31 more
  • JavaScript 運算子

    JavaScript 運算子/運算子 在 JavaScript 中,有一些運算子可以使代碼更簡潔、易讀和高效。以下是一些常見的運算子: 1、可選鏈運算子(optional chaining operator) ?.是可選鏈運算子(optional chaining operator)。?. 可選鏈操 ......

    uj5u.com 2023-04-20 08:02:25 more
  • CSS—相對單位rem

    一、概述 rem是一個相對長度單位,它的單位長度取決于根標簽html的字體尺寸。rem即root em的意思,中文翻譯為根em。瀏覽器的文本尺寸一般默認為16px,即默認情況下: 1rem = 16px rem布局原理:根據CSS媒體查詢功能,更改根標簽的字體尺寸,實作rem單位隨螢屏尺寸的變化,如 ......

    uj5u.com 2023-04-20 08:02:21 more
  • 我的第一個NPM包:panghu-planebattle-esm(胖虎飛機大戰)使用說明

    好家伙,我的包終于開發完啦 歡迎使用胖虎的飛機大戰包!! 為你的主頁添加色彩 這是一個有趣的網頁小游戲包,使用canvas和js開發 使用ES6模塊化開發 效果圖如下: (覺得圖片太sb的可以自己改) 代碼已開源!! Git: https://gitee.com/tang-and-han-dynas ......

    uj5u.com 2023-04-20 08:01:50 more
  • 如何在 vue3 中使用 jsx/tsx?

    我們都知道,通常情況下我們使用 vue 大多都是用的 SFC(Signle File Component)單檔案組件模式,即一個組件就是一個檔案,但其實 Vue 也是支持使用 JSX 來撰寫組件的。這里不討論 SFC 和 JSX 的好壞,這個仁者見仁智者見智。本篇文章旨在帶領大家快速了解和使用 Vu ......

    uj5u.com 2023-04-20 08:01:37 more
  • 【Vue2.x原始碼系列06】計算屬性computed原理

    本章目標:計算屬性是如何實作的?計算屬性快取原理以及洋蔥模型的應用?在初始化Vue實體時,我們會給每個計算屬性都創建一個對應watcher,我們稱之為計算屬性watcher ......

    uj5u.com 2023-04-20 08:01:31 more
  • http1.1與http2.0

    一、http是什么 通俗來講,http就是計算機通過網路進行通信的規則,是一個基于請求與回應,無狀態的,應用層協議。常用于TCP/IP協議傳輸資料。目前任何終端之間任何一種通信方式都必須按Http協議進行,否則無法連接。tcp(三次握手,四次揮手)。 請求與回應:客戶端請求、服務端回應資料。 無狀態 ......

    uj5u.com 2023-04-20 08:01:10 more
  • http1.1與http2.0

    一、http是什么 通俗來講,http就是計算機通過網路進行通信的規則,是一個基于請求與回應,無狀態的,應用層協議。常用于TCP/IP協議傳輸資料。目前任何終端之間任何一種通信方式都必須按Http協議進行,否則無法連接。tcp(三次握手,四次揮手)。 請求與回應:客戶端請求、服務端回應資料。 無狀態 ......

    uj5u.com 2023-04-20 08:00:32 more