问题描述
我正在尝试在客户端加载两个day.js
插件。但这似乎不起作用...我在做什么错?
Pug / Jade
文件
script(defer,src='javascript/external/day.js')
script(defer,src='javascript/external/day_minmax.js')
script(defer,src='javascript/external/day_isbetween.js')
script(defer,dayjs.extend(window.dayjs_plugin_minmax))
script(defer,dayjs.extend(window.dayjs_plugin_isbetween))
console
输出
dayjs.max()
Uncaught TypeError: dayjs.max is not a function
https://unpkg.com/dayjs@1.9.1/plugin/isBetween.js
https://unpkg.com/dayjs@1.9.1/plugin/minMax.js
解决方法
这是index.html文件中的一个有效示例,您可以在根Pug / Jade文件中执行相同的操作。另外,我使用的是CDN版本,但是您也可以从下载它们的文件夹中导入它们。
import cv2,numpy as np
import time,random,threading,queue
image_height = 256
image_width = 512
save_nsec = 20
dpi,fps = 100,15
# Make results reproducible and deterministic
np.random.seed(0)
random.seed(0)
def live_stream():
last = 0.
pos = 0
while True:
a = np.random.uniform(low = -1.,high = 1.,size = random.randint(1,30)).astype(np.float64).cumsum() + last
yield a,pos,pos + a.size - 1
pos += a.size
last = a[-1]
time.sleep(random.random() * 2.2 / fps)
q0 = queue.Queue()
def stream_fetcher():
for e in live_stream():
q0.put(e)
threading.Thread(target = stream_fetcher,daemon = True).start()
aranges = np.arange(image_height,dtype = np.int32)[:,None]
q1 = queue.Queue()
def renderer():
def normalized(data,data_min,data_max,maximum_value):
nomamized_data = maximum_value * ((data - data_min) / (data_max - data_min))
return nomamized_data
prev_image = np.zeros((image_height,0),dtype = np.uint8)
prev_vols = np.zeros((0,),dtype = np.float64)
while True:
data = []
data.append(q0.get())
try:
while True:
data.append(q0.get(block = False))
except queue.Empty:
pass
data_vols = [e[0] for e in data]
data_minx,data_maxx = data[0][1],data[-1][2]
vols = np.concatenate(data_vols)[-image_width:]
prev_vols = prev_vols[-(image_width - vols.size) or prev_vols.size:]
concat_vols = np.concatenate((prev_vols,vols))[-image_width:]
vols_min,vols_max = np.amin(concat_vols),np.amax(concat_vols)
if prev_vols.size > 0 and (vols_min < np.amin(prev_vols) - 10 ** -8 or vols_max > np.amax(prev_vols) + 10 ** -8):
vols = concat_vols
prev_image = prev_image[:,:-prev_vols.size]
prev_vols = prev_vols[:0]
vols_norm = normalized(
data = vols,data_min = vols_min,data_max = vols_max,maximum_value = image_height,)
image = (aranges < vols_norm.astype(np.int32)[None,:]).astype(np.uint8) * 255
whole_image = np.concatenate((prev_image,image),axis = 1)[:,-image_width:]
q1.put((whole_image,data_maxx - whole_image.shape[1] + 1,data_maxx,vols_min,vols_max))
prev_image = whole_image
prev_vols = concat_vols
threading.Thread(target = renderer,daemon = True).start()
def visualizer():
import matplotlib.pyplot as plt,matplotlib.animation
def images():
while True:
data = []
data.append(q1.get())
try:
while True:
data.append(q1.get(block = False))
except queue.Empty:
pass
minx = min([e[1] for e in data])
maxx = min([e[2] for e in data])
miny = min([e[3] for e in data])
maxy = min([e[4] for e in data])
image = np.concatenate([e[0] for e in data],-image_width:]
image = np.pad(image,((0,(image_width - image.shape[1],0)),constant_values = 0)
image = np.repeat(image[:,:,None],3,axis = -1)
yield image,minx,maxx,miny,maxy
it = images()
im = None
fig = plt.figure(figsize = (image_width / dpi,image_height / dpi),dpi = dpi)
def animate_func(i):
nonlocal it,im,fig
image,maxy = next(it)
print(f'.',end = '',flush = True)
if im is None:
im = plt.imshow(image,interpolation = 'none',aspect = 'auto')
else:
im.set_array(image)
im.set_extent((minx,maxy))
return [im]
anim = matplotlib.animation.FuncAnimation(fig,animate_func,frames = round(save_nsec * fps),interval = 1000 / fps)
print('saving...',flush = True)
#anim.save('result.mp4',fps = fps,dpi = dpi,extra_args = ['-vcodec','libx264'])
anim.save('result.gif',writer = 'imagemagick')
print('saved!',flush = True)
plt.show()
threading.Thread(target = visualizer,daemon = True).start()
while True:
time.sleep(0.1)
与window.dayjs_plugin_minmax
可能是问题
window.dayjs_plugin_minMax
这是结果: