• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Zooming Web Pages
2
3ArkWeb supports gesture zoom, mouse wheel zoom, and keyboard zoom, allowing users to adjust the display to a comfortable size. It also provides the capability of listening for and controlling the page zoom ratio for applications to achieve personalized visual effects.
4
5## Setting Web Page Zoom
6
7### Setting Gesture Zoom
8
9You can use the [zoomAccess](../reference/apis-arkweb/arkts-basic-components-web-attributes.md#zoomaccess) attribute to set the web page zoom functionality. When this attribute is set to **false**, the web page cannot be zoomed in or out using gestures.
10
11When **<meta name="viewport" id="viewport" content="user-scalable=no">** is set for the HTML web page, the web page cannot be zoomed in or out using gestures.
12
13Gestures can be used to zoom in or out only when both the **zoomAccess** and **viewport** tags are set to allow zooming.
14
15> **NOTE**
16>
17> On a PC or 2-in-1 device, the **viewport** tag does not take effect. You can only set **zoomAccess** to **false** to disable gesture zoom.
18>
19> The preceding tags can only be used to enable or disable the zoom functionality. If **minimum-scale** and **maximum-scale** are set in the **viewport** tag, the zoom range is also restricted by the two attributes. When the maximum and minimum values are the same, the web page cannot be zoomed in or out. Currently, ArkWeb does not provide the capability of forcibly zooming in or out a page.
20
21```ts
22// xxx.ets
23import { webview } from '@kit.ArkWeb';
24
25@Entry
26@Component
27struct WebComponent {
28  controller: webview.WebviewController = new webview.WebviewController();
29
30  build() {
31    Column() {
32      Web({ src: 'www.example.com', controller: this.controller })
33        .zoomAccess(false)
34    }
35  }
36}
37```
38
39Loaded HTML:
40```html
41<!DOCTYPE html>
42<html lang="en">
43<head>
44    <meta charset="UTF-8">
45    <meta name="viewport" content="user-scalable=yes">
46    <title>Document</title>
47</head>
48<body>
49    <p>this is a test page</p>
50</body>
51</html>
52```
53
54### Setting Keyboard and Mouse Wheel Zoom
55
56By default, ArkWeb supports zooming by pressing the **Ctrl**+**'-'/'+'** keys or using the mouse wheel with the **Ctrl** key. An application can intercept keyboard events to disable keyboard zoom. Currently, the application cannot disable mouse wheel zoom.
57
58Example of intercepting keyboard events to disable keyboard zoom:
59
60```ts
61// xxx.ets
62import { webview } from '@kit.ArkWeb';
63import { KeyCode } from '@kit.InputKit';
64
65@Entry
66@Component
67struct WebComponent {
68  controller: webview.WebviewController = new webview.WebviewController();
69
70  build() {
71    Column() {
72      Web({ src: 'www.example.com', controller: this.controller })
73        .zoomAccess(true)
74        .onKeyPreIme((event) => {
75          if (event.type == KeyType.Down &&
76              event.getModifierKeyState &&
77              event.getModifierKeyState(['Ctrl']) &&
78              (event.keyCode == KeyCode.KEYCODE_MINUS || event.keyCode == KeyCode.KEYCODE_EQUALS) ||
79               event.keyCode == KeyCode.KEYCODE_NUMPAD_SUBTRACT || event.keyCode == KeyCode.KEYCODE_NUMPAD_ADD) {
80            return true;
81          }
82          return false;
83        })
84    }
85  }
86}
87```
88
89## Listening for Page Zoom Ratio Changes
90
91An application can listen for page zoom ratio changes through the [onScaleChange](../reference/apis-arkweb/arkts-basic-components-web-events.md#onscalechange9) API.
92This API event corresponds to the gesture event (zoom with two fingers). **event.newScale** corresponds to the web page attribute **visualViewport.scale**.
93
94```ts
95// xxx.ets
96import { webview } from '@kit.ArkWeb';
97
98@Entry
99@Component
100struct WebComponent {
101  controller: webview.WebviewController = new webview.WebviewController();
102
103  build() {
104    Column() {
105      Web({ src: 'www.example.com', controller: this.controller })
106        .onScaleChange((event) => {
107          console.log('onScaleChange changed from ' + event.oldScale + ' to ' + event.newScale);
108        })
109    }
110  }
111}
112```
113
114## Controlling the Page Zoom Ratio
115
116You can call the [zoom](../reference/apis-arkweb/arkts-apis-webview-WebviewController.md#zoom), [zoomIn](../reference/apis-arkweb/arkts-apis-webview-WebviewController.md#zoomin) and [zoomOut](../reference/apis-arkweb/arkts-apis-webview-WebviewController.md#zoomout) APIs to control the page zoom ratio.
117
118 > **NOTE**
119 >
120 > When the **zoom** class APIs are used to control page zooming, you must set **zoomAccess** to **true**. Otherwise, the **zoom** class APIs throw exception 17100004.
121
122### Zooming at Fixed Ratios
123
124You can use **zoomIn** to zoom in the current web page by 25% or **zoomOut** to zoom out the current web page by 20%.
125
126```ts
127// xxx.ets
128import { webview } from '@kit.ArkWeb';
129import { BusinessError } from '@kit.BasicServicesKit';
130
131@Entry
132@Component
133struct WebComponent {
134  controller: webview.WebviewController = new webview.WebviewController();
135  build() {
136    Column() {
137      Button('zoomIn')
138        .onClick(() => {
139          try {
140            this.controller.zoomIn();
141          } catch (error) {
142            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
143          }
144        })
145      Button('zoomOut')
146        .onClick(() => {
147          try {
148            this.controller.zoomOut();
149          } catch (error) {
150            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
151          }
152        })
153      Web({ src: 'www.example.com', controller: this.controller })
154        .zoomAccess(true)
155    }
156  }
157}
158```
159
160### Controlling Zoom Ratios Based on Input Values
161
162You can use **zoom** to zoom in or out on the current web page. When the input parameter is set to **1**, the default page size is used. When the input parameter is set to a value less than 1, the page is zoomed out. When the input parameter is set to a value greater than 1, the page is zoomed in. The value must be greater than 0.
163
164```ts
165// xxx.ets
166import { webview } from '@kit.ArkWeb';
167import { BusinessError } from '@kit.BasicServicesKit';
168
169@Entry
170@Component
171struct WebComponent {
172  controller: webview.WebviewController = new webview.WebviewController();
173  @State factor: number = 1;
174
175  build() {
176    Column() {
177      TextInput()
178        .type(InputType.NUMBER_DECIMAL)
179        .onChange((value)=>{
180            this.factor = Number(value);
181        })
182      Button('zoom')
183        .onClick(() => {
184          try {
185            this.controller.zoom(this.factor);
186          } catch (error) {
187            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
188          }
189        })
190      Web({ src: 'www.example.com', controller: this.controller })
191        .zoomAccess(true)
192    }
193  }
194}
195```
196![zoom-by-step](./figures/zoom-by-step.gif)
197
198### Zooming to Target Ratios
199
200You can call the **onScaleChange** API to obtain the current page zoom ratio, and then call the **zoom** API to zoom in or out the page to the specified ratio. You can calculate the input parameter of **zoom** based on **pageFactor** and **targetFactor** as follows:
201
202```
203factor = 100 * targetFactor / pageFactor
204```
205
206```ts
207// xxx.ets
208import { webview } from '@kit.ArkWeb';
209import { BusinessError } from '@kit.BasicServicesKit';
210
211@Entry
212@Component
213struct WebComponent {
214  controller: webview.WebviewController = new webview.WebviewController();
215  @State targetFactor: number = 1;
216  @State pageFactor: number = 100;
217
218  build() {
219    Column() {
220      TextInput()
221        .type(InputType.NUMBER_DECIMAL)
222        .onChange((value)=>{
223          this.targetFactor = Number(value);
224        })
225      Button('zoom')
226        .onClick(() => {
227          try {
228            let factor = this.targetFactor * 100 / this.pageFactor;
229            this.controller.zoom(factor);
230          } catch (error) {
231            console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as BusinessError).message}`);
232          }
233        })
234      Web({ src: 'www.example.com', controller: this.controller })
235        .zoomAccess(true)
236        .onScaleChange((event) => {
237          console.log('onScaleChange changed from ' + event.oldScale + ' to ' + event.newScale);
238          this.pageFactor = event.newScale;
239        })
240    }
241  }
242}
243```
244![zoom-to-target](./figures/zoom-to-target.gif)
245